jquery fadeIn fadeOut效果不起作用

时间:2017-07-16 08:49:47

标签: javascript jquery html css

我正在尝试学习jquery,我想创建循环通过图像的fadein和fadeout效果。我使用了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>

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

&#13;
&#13;
1       2       3       4
5       6       7       8
9       10      11      12
13      14      15      16
17  
&#13;
$(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);
});
&#13;
&#13;
&#13;