【问题标题】:Divs appearing all at once rather than individuallyDivs 一次出现而不是单独出现
【发布时间】:2018-05-14 23:09:36
【问题描述】:

我在一个容器中有一组 div。我希望这些 div 一个一个出现在 div 的顶部,使用淡入然后淡出效果。目前我正在使用 for 循环来尝试实现这一点,但结果很奇怪。它不是遍历类元素,而是一次显示它们。我一直试图找出问题所在,但我似乎无法找到问题所在。

var customerComments = $(".customercomment");

for (var i = 0; i < customerComments.length; i++) {
  $(customerComments[i]).fadeIn("slow", function() {
    $(customerComments[i]).delay(600).fadeOut("slow");
  })
}
/*STYLING FOR TESTIMONIALS SECTION */

#customerreviews {
  background-color: #5a2a27;
  min-height: 300px;
}

.customercomment {
  margin-top: 20px auto;
  padding: 20px 120px;
  text-align: center;
  display: none;
}

.customercomment p {
  font-size: 30px;
  line-height: 45px;
}

.customercomment h4 {
  border-top: 1px solid white;
  margin: 0 auto;
  width: 50%;
  padding-top: 20px;
}

.location {
  font-size: 20px;
  font-style: italic;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<section id="testimonials">
  <div class="container">
    <div class="row text-center">
      <div class="col-md-12">
        <h1>TESTIMONIALS FROM OUR MEMBERS</h1>
      </div>
    </div>
    <div class="row text-center">
      <div class="col-md-12" id="customerreviews">
        <div class="customercomment text-center">
          <p>"What I find to be the best thing about Elite Fitness Gym, other than the facilities they have to offer, is their overall service and the fact that they do not tie you down to a contract. They make things as easy as possible, inside and outside
            the gym, in every way!"</p>
          <h4>Rachel Smith<span class="location"> (Manchester)</span></h4>
        </div>
        <div class="customercomment text-center">
          <p>"I am blown away at how cheap the membership is and how much we get back in return. I take full advantage of the MMA classes they offer! I also like the fact that there is more than one class per week, which offers me flexibility - something
            I need with my busy schedule."</p>
          <h4>Darren Wise<span class="location"> (London)</span></h4>
        </div>
        <div class="customercomment text-center">
          <p>"I am really impressed with the amount of equipment Elite Fitness Gym offer. Even during rush hour I rarely find myself waiting around for equipment. They have certainly done a fantastic job there. I like to keep my workouts short but intense,
            so its a great benefit for me."</p>
          <h4>Arif Akhtar<span class="location"> (Leeds)</span></h4>
        </div>
        <div class="customercomment text-center">
          <p>"I've been around the block with a number of gyms, but what sets Elite Fitness Gym apart from the rest is their ability to always improve on their service and benefits. I dont feel the need to look elsewhere because this gym is always growing
            and taking fantastic, ongoing care of its members."</p>
          <h4>Jason Hardy<span class="location"> (London)</span></h4>
        </div>
        <div class="customercomment text-center">
          <p>"I am a big lover of swimming and the facilities that Elite Fitness Gym offer, not just with resistance workouts, but also with cardio, is what encouraged me to join in the first place. I must say that I am so happy with the decision as they
            really do have great facilities here."</p>
          <h4>Michelle Rousey<span class="location"> (Birmingham)</span></h4>
        </div>
        <div class="customercomment text-center">
          <p>"The thing I benefit from most about Elite Fitness Gym is their timings. They are open 24 hours a day so it gives me a chance to drop in and do my workout just before going to work on my nightshifts. I am grateful to be able to attend such a
            great gym during those hours of the day."</p>
          <h4>Jermaine Clarke<span class="location"> (Bristol)</span></h4>
        </div>
      </div>
    </div>
  </div>
</section>

感谢您的帮助!

【问题讨论】:

  • 旁注:$(customerComments[i]) 正在获取一个 jquery 对象,分解单个元素,然后将其转回一个 jquery 对象。只需使用customerComments.eq(i) 即可获得相同的结果。
  • 另外,为了深入了解您的思维过程,您认为哪一部分逻辑可以防止它们同时消失?
  • 我对编程还是很陌生,但我对使用循环的期望是,在每个场合它都会针对 0 索引,然后是 dom 集合的第一个 1 索引。我的想法是,一旦完成将效果应用于包含类名的第一个元素,它将移至下一个元素。这不是它如何与 javascript 中的循环一起工作吗?还是将它们与 jquery 一起使用时会有所不同?抱歉,如前所述,我对编程还是很陌生,所以我意识到我的不理解有时会有点混乱
  • 如果您希望每个 div 按顺序出现,您需要以某种方式“链接”异步动画,使 div "n" 等待 div "n-1" 动画完成.您的 jquery 版本在 fadeIn 函数的返回值上有“jquery promise”属性吗?
  • fadeIn/fadeOut 方法是非阻塞方法。他们在 jQuery 用来执行动画的内部动画队列上创建条目。在处理这些循环时,循环将继续运行。所以,对于解决方案。您希望逻辑在前一个淡入后继续,还是在前一个淡入后继续?

标签: javascript jquery html css


【解决方案1】:

我测试了以下内容,它会逐个淡化 div。虽然它不使用fadeOut 函数,但它仍然使用fadeIn 函数。

$($(".customercomment").get().reverse()).each(function(fadeInDiv) {
     $(this).delay(fadeInDiv * 600).fadeIn(1000);
});

【讨论】:

    【解决方案2】:

    我能想到的最简单的方法是使用动画函数的返回值中可用的承诺,并使用 array#reduce 链接它们

    var customerComments = $(".customercomment");
    
    function doit() {
        [].reduce.call(customerComments, function(p, unused, i) {
            var $comment = customerComments.eq(i);
            return p.then(function() {
                return $comment.fadeIn("slow").promise().then(function() {
                    return $comment.delay(600).fadeOut("slow").promise();
                });
            });
        }, $.when()).then(doit);
    }
    doit();
    /*STYLING FOR TESTIMONIALS SECTION */
    
    #customerreviews {
      background-color: #5a2a27;
      min-height: 300px;
    }
    
    .customercomment {
      margin-top: 20px auto;
      padding: 20px 120px;
      text-align: center;
      display: none;
    }
    
    .customercomment p {
      font-size: 30px;
      line-height: 45px;
    }
    
    .customercomment h4 {
      border-top: 1px solid white;
      margin: 0 auto;
      width: 50%;
      padding-top: 20px;
    }
    
    .location {
      font-size: 20px;
      font-style: italic;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <section id="testimonials">
      <div class="container">
        <div class="row text-center">
          <div class="col-md-12">
            <h1>TESTIMONIALS FROM OUR MEMBERS</h1>
          </div>
        </div>
        <div class="row text-center">
          <div class="col-md-12" id="customerreviews">
            <div class="customercomment text-center">
              <p>"What I find to be the best thing about Elite Fitness Gym, other than the facilities they have to offer, is their overall service and the fact that they do not tie you down to a contract. They make things as easy as possible, inside and outside
                the gym, in every way!"</p>
              <h4>Rachel Smith<span class="location"> (Manchester)</span></h4>
            </div>
            <div class="customercomment text-center">
              <p>"I am blown away at how cheap the membership is and how much we get back in return. I take full advantage of the MMA classes they offer! I also like the fact that there is more than one class per week, which offers me flexibility - something
                I need with my busy schedule."</p>
              <h4>Darren Wise<span class="location"> (London)</span></h4>
            </div>
            <div class="customercomment text-center">
              <p>"I am really impressed with the amount of equipment Elite Fitness Gym offer. Even during rush hour I rarely find myself waiting around for equipment. They have certainly done a fantastic job there. I like to keep my workouts short but intense,
                so its a great benefit for me."</p>
              <h4>Arif Akhtar<span class="location"> (Leeds)</span></h4>
            </div>
            <div class="customercomment text-center">
              <p>"I've been around the block with a number of gyms, but what sets Elite Fitness Gym apart from the rest is their ability to always improve on their service and benefits. I dont feel the need to look elsewhere because this gym is always growing
                and taking fantastic, ongoing care of its members."</p>
              <h4>Jason Hardy<span class="location"> (London)</span></h4>
            </div>
            <div class="customercomment text-center">
              <p>"I am a big lover of swimming and the facilities that Elite Fitness Gym offer, not just with resistance workouts, but also with cardio, is what encouraged me to join in the first place. I must say that I am so happy with the decision as they
                really do have great facilities here."</p>
              <h4>Michelle Rousey<span class="location"> (Birmingham)</span></h4>
            </div>
            <div class="customercomment text-center">
              <p>"The thing I benefit from most about Elite Fitness Gym is their timings. They are open 24 hours a day so it gives me a chance to drop in and do my workout just before going to work on my nightshifts. I am grateful to be able to attend such a
                great gym during those hours of the day."</p>
              <h4>Jermaine Clarke<span class="location"> (Bristol)</span></h4>
            </div>
          </div>
        </div>
      </div>
    </section>

    【讨论】:

    • 感谢您提供此解决方案 Jaromanda!我很抱歉,但还有一种方法可以循环这个方法吗?
    • 哦,你想让它不断循环遍历那些 div 吗?
    • 确实是 Jaromanda,抱歉,如果我在我的 OP 中不清楚这一点,那是我的错误
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 2021-10-31
    • 1970-01-01
    相关资源
    最近更新 更多