简体   繁体   English

同时提高第一div的不透明度。 和循环

[英]raise opacity for first div at same time reduce for second. And looping

I have 2 divs with background-image. 我有2个具有背景图像的div。 I want to raise at the same time opacity for first div and reduce opacity of another. 我想同时提高第一格的不透明度,并减少另一个的不透明度。 And I want to loop that. 我想循环一下。 I want to get the smoothly effect of shifts backgrounds. 我想获得轮班背景的流畅效果。 Is there such a way to do that? 有这种方法吗?

<div class="first"></div>
<div class="second"></div>

.first, .second {
    position: absolute;
    height: 100%;
    width: 100%;
}
.first {
    background: url(http://placehold.it/350x150);
    background-size: cover;
    opacity: 0;
}
.second {
    background: url(http://placehold.it/250x150);
    background-size: cover;
    opacity: 1;
}

jsfiddle 的jsfiddle

Sure you can. 你当然可以。 Create a function, use jQuery.animate() in chain on each element to animate its opacity to the inverse of its value. 创建一个函数,在每个元素的链中使用jQuery.animate()将其不透明度动画化为其值的倒数。 Use the function itself as a callback of the second animate function. 使用函数本身作为第二个动画函数的回调。

<div class="box" id="box1"></div>
<div class="box" id="box2"></div>

var box = $('.box');

function animateOpacity() {

    box.each(function () {

        var opacity = parseInt($(this).css('opacity'));

        $(this).animate({
            opacity: 1 - opacity
        }, 800).animate({
            opacity: opacity
        }, 800, animateOpacity);
    });
}

JSFiddle demo JSFiddle演示


I would do something like that: 我会做这样的事情:

So there is an active class that changes the opacity and visibility of the imgHolder . 因此,有一个active类可以更改imgHolder的不透明度和可见性。 If you click on the first image it removes the active class on that element and jumps to the next element in the DOM and adds there the active class. 如果单击第一个图像,它将删除该元素上的active类,并跳转到DOM中的下一个元素,并在其中添加active类。 You could get be more save if you write next('imgHolder') so that only the next element with class imgHolder gets the active class. 如果您编写next('imgHolder') ,则可以节省更多,这样只有下一个具有imgHolder类的元素imgHolder获得active类。

I also add a loop. 我还添加了一个循环。 So with the length function it counts how much elements with imgHolder are in the DOM. 因此,使用length函数可以计算imgHolder中DOM中的元素数量。 In this case 2. So after the second is active it goes to the first imgHolder . 在这种情况下,2。因此在第二个active后,它转到第一个imgHolder

You can see it in action here: http://jsfiddle.net/rcc6y1nn/2/ 您可以在这里查看其运行情况: http : //jsfiddle.net/rcc6y1nn/2/

I hope you want something like that. 我希望你想要那样的东西。

UPDATE UPDATE
Now with setInterval the class changes after one second in a loop. 现在,使用setInterval ,类将在循环一秒钟后更改。



HTML HTML

<div class="imgHolder first"></div>
<div class="imgHolder second"></div>


CSS CSS

.imgHolder {
    position: absolute;
    top: 0;
    opacity: 0;
    visibility: hidden;
    width: 350px;
    height: 350px;
    -webkit-transition: all 150ms ease-in;
    transition: all 150ms ease-in;
}
.imgHolder.first {
    background: url('http://lorempixel.com/output/technics-q-c-350-350-10.jpg');
}
.imgHolder.second {
    background: url('http://lorempixel.com/output/animals-q-c-350-350-5.jpg');
}
.imgHolder.active {
    opacity: 1;
    visibility: visible;
}


Javascript 使用Javascript

var $imgHolder = $('.imgHolder');

$imgHolder.first().addClass('active');

setInterval(function(){
    var $next = $('.imgHolder.active').removeClass('active').next();
    if ($next.length) {
        $next.addClass('active');
    } else {
        $imgHolder.first().addClass('active');
    }
}, 1000);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用jQuery降低div其他元素的不透明度 - Using jQuery to reduce opacity of other elements with a div 隐藏第一个表格,然后显示第二个。 二级表单执行多次提交 - Hiding first form and then displaying second. Secnd form performs multiple submits 使从API刻度中调用的日期/时间值第二秒。 (JavaScript) - Make a date/time value called from an API tick very second. (JavaScript) 如何为具有不透明度的 div 设置时间? - How to set time for a div with opacity? 循环使用同一类的每个div一次更改颜色 - Looping each div with same class to change color at a time 第二个按钮不会在与第一个按钮相同的 div 中生成表单 - Second button doesn't produce form in same div as first button 两个点击功能,影响同一个div,第一个有效,第二个无效 - javascript - Two click functions, affecting the same div, the first work but second not - javascript Array.map() 仅适用于第一个数组,但不适用于第二个数组。 有人可以解释为什么它不起作用并提供适当的解决方案 - Array.map( ) only works on First Array but not on the Second. Can someone please explain why it doesn't work and provide proper solution 降低水平滚动中 div 的不透明度是否部分可见 - Reduce opacity of a div in a horizontal scroll is it's partially visible 尝试在两次迭代中警告同一选择器的值,在第一次迭代中获得正确的值,而在第二次迭代中获得不正确的值。 为什么? - Trying to alert value of same selector in two iterations, getting correct value in 1st iteration and incorrect one in second. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM