jQuery对所有元素进行动画处理,而不是仅对下一个元素进行动画处理

时间:2020-02-21 10:31:32

标签: jquery css

当我单击.typeform元素时,我试图将动画应用于.nexto元素。使用toggleClass时,所有.typeform都受到toggleClass的影响。

单击.typeform时,需要将动画应用于下一个.nexto元素。请帮我。谢谢。

$('.nexto').on('click', function(event) {
  $('.typeform-element').toggleClass('animations');
})
.animations {
  animation: animation-1 1s linear;
}

@keyframes animation-1 {
  0% {
    top: -50px;
    opacity: 0;
  }
  50% {
    top: -25px;
    opacity: 0.5;
  }
  100% {
    top: 0px;
    opacity: 1;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="typeform">
  <div class="typeform-element" id="fname">
    <p><span class="list">1</span>&#8594;What's your First Name? *</p>
    <input type="text" name="fname" placeholder="Type your answer..." autofocus>
    <div class="ok-btn">
      <a href="#lname" class="nexto">OK &#10004;</a>
    </div>
  </div>
  <div class="typeform-element" id="lname">
    <p><span class="list">2</span>&#8594;What's Your Last Name? *</p>
    <input type="text" name="lname" placeholder="Type your answer...">
    <div class="ok-btn">
      <a href="#email" class="nexto">OK	&#10004;</a>
    </div>
  </div>
  <div class="typeform-element" id="email">
    <p><span class="list">3</span>&#8594;Let's know your Email Address*</p>
    <input type="email" name="email" placeholder="Type your answer...">
    <div class="ok-btn">
      <a href="#submit" class="nexto">OK &#10004;</a>
    </div>
  </div>
  <div class="typeform-element" id="submit">
    <div class="ok-btn">
      <a href="#fname">Submit &#10004;</a>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:3)

您需要访问父按钮,然后选择紧随其后的同级按钮,并仅切换其班级。

$('.nexto').on('click', function(event){
           $(this).closest('.typeform-element').next('.typeform-element').toggleClass('animations');
    });