【问题标题】:jquery fadeIn fadeOut effect not workingjquery fadeIn fadeOut 效果不起作用
【发布时间】:2017-07-17 04:44:42
【问题描述】:

我正在尝试学习 jquery,我想创建循环播放图像的淡入淡出效果。我使用 setInterval 函数来循环遍历图像,但它仅适用于第一次迭代。

$(document).ready(function() {
      var newcount = 0;
      var image = $(".image-store img");
      image.hide();
      var image_array = [];
      image.each(function() {
        image_array.push($(this).attr('src'));
      });
      var showimg = $(".image-disp img");
      showimg.attr("src", image_array[newcount]);

      setInterval(function() {
        showimg.fadeOut(2000, function() {
          newcount = newcount + 1;
          showimg.attr("src", image_array[newcount]);
          showimg.fadeIn(2000);
        });
        if (newcount == image_array.length) {
          newcount = -1;
        }
      }, 3000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="image-store">
  <img src="img/img1.jpg">
  <img src="img/img2.jpg">
  <img src="img/img3.jpg">
</div>
<div class="image-disp">
  <img src="">
</div>

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    它有效,但您在代码末尾缺少});。使用浏览器控制台了解是否有错误。

    【讨论】:

      【解决方案2】:

      $(document).ready(function() {
        var newcount = 0;
        var image = $(".image-store img");
        image.hide();
        var image_array = [];
        image.each(function() {
          image_array.push($(this).attr('src'));
        });
        var showimg = $(".image-disp img");
        showimg.attr("src", image_array[newcount]);
      
        setInterval(function() {
          showimg.fadeOut(2000, function() {
            newcount = newcount + 1;
            showimg.attr("src", image_array[newcount]);
            showimg.fadeIn(2000);
          });
          if (newcount == image_array.length) {
            newcount = -1;
          }
        }, 3000);
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="image-store">
        <img width="50" src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
        <img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
        <img width="50" src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
      </div>
      <div class="image-disp">
        <img width="200" src="">
      </div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多