首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FadeIn和FadeOut互为不同的背景

FadeIn和FadeOut互为不同的背景
EN

Stack Overflow用户
提问于 2018-11-29 18:15:53
回答 2查看 61关注 0票数 2

我下面的例子的问题是,最后一个背景没有像setInterval函数中计划的那样显示6秒,而是立即消失在第一个背景中。

当然,我希望它的行为与它们的其余部分一样。我不认为它改变了任何东西,但我的真实例子有图像的背景,而不是颜色,只是为了让这个例子更容易一些。

提前感谢您的帮助和时间!

代码语言:javascript
复制
var screen = 0,
  speed = 1000;

window.setInterval(function() {
  $(".background-" + ((screen % 4) + 1)).fadeOut(speed,
    function() {
      $(".background-" + (((screen + 1) % 4) + 1)).fadeIn(speed);
    });
  screen += 1
}, 6000);
代码语言:javascript
复制
.background-1 {
  background: red;
  z-index: 6;
  position: absolute;
}

.background-2 {
  background: blue;
  z-index: 5;
  position: absolute;
}

.background-3 {
  background: green;
  z-index: 4;
  position: absolute;
}

.background-4 {
  background: yellow;
  z-index: 3;
  position: absolute;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="background background-1">

  </div>
  <div class="background background-2">

  </div>
  <div class="background background-3">

  </div>
  <div class="background background-4">

  </div>
</div>

EN

回答 2

Stack Overflow用户

发布于 2018-11-29 18:30:52

代码语言:javascript
复制
var screen = 0,
    speed = 1000;

    window.setInterval(function() {
       $(".background-" + ((screen % 4) + 1)).fadeOut(speed, 
       function() {
           $(".background-" + (((screen + 1) % 4) + 1)).fadeIn(speed);
             screen+=1
       });
    },6000);
代码语言:javascript
复制
.background-1 {
       background: red;
       z-index: 6;
       position: absolute;
    }
   .background-2 {
       background: blue;
       z-index: 5;
       position: absolute;
   }
   .background-3 {
       background: green;
       z-index: 4;
       position: absolute;
   }
   .background-4 {
       background: yellow;
       z-index: 3;
       position: absolute;
   }
   .wrapper{
   width:400px;
   }
   .background {
    width: 200px;
    height: 200px;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<div class="wrapper">
                <div class="background background-1">

                </div>
                <div class="background background-2">

                </div>
                <div class="background background-3">

                </div>
                <div class="background background-4">

                </div>
            </div>

screen的回调函数中设置变量fadeout增量。

票数 2
EN

Stack Overflow用户

发布于 2018-11-29 18:34:04

代码语言:javascript
复制
<script>
    $(function () {
        var speed = 1000;
        var counter = 0
        var divlist = $('.background-1, .background-2, .background-3,.background-4');

        function showDiv() {
            divlist.fadeOut(speed)
                .filter(function (index) { return index == counter % 3; })
                .fadeIn(speed);

            counter++;
        };

        showDiv();

        setInterval(function () {
            showDiv();
        }, 6000);
    });
</script>


<div class="wrapper">
    <div class="background background-1">
        111111111111111111
    </div>
    <div class="background background-2">
        22222222222222222222
    </div>
    <div class="background background-3">
        33333333333333333333333333
    </div>
    <div class="background background-4">
        4444444444444444444444444
    </div>
</div>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53536615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档