简体   繁体   English

元素之间平滑过渡

[英]Transitioning smoothly between elements

I had a go at writing some jquery that I plan to use on a multi step form. 我可以写一些计划在多步骤表单上使用的jquery。 It works but the transitions are glitchy. 它有效,但过渡时出现故障。

If I just go back and forth between 1 & 2 then it seems ok but if I go 1-3 then go in reverse it doesn't work as expected. 如果我只是在1和2之间来回移动,那似乎还可以,但是如果我进行1-3,然后反向进行,则无法正常工作。 Sometimes click doesn't register and it all seems to get out of timing. 有时,点击没有注册,这似乎不合时宜。

What is a better approach here to make this look consistent? 有什么更好的方法可以使外观看起来一致?

 $(function() { $('.next-form').click(function() { current_fs = $(this).parent(); next_fs = $(this).parent().next(); $(current_fs).animate({ opacity: 0 }, "fast", function() { $(this).hide(); }); $(next_fs).css({ position: 'absolute', left: '100%', top: 0 }); $(next_fs).show(function() { $(this).animate({ left: 0 }) }); }); $('.previous-form').click(function() { current_fs = $(this).parent(); previous_fs = $(this).parent().prev(); $(current_fs).animate({ left: '100%' }, function() { $(this).hide(); }); $(previous_fs).show(function() { $(this).animate({ opacity: 100 }, 2000); }) }); }); 
 .outer{ position: relative; width: 500px; height: 50px; overflow: hidden; } .one{ width: 500px; background-color: #000; height: 50px; } .two{ width: 500px; background-color: blue; height: 50px; display: none; } .three{ width: 500px; background-color: red; height: 50px; display: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <div class="outer"> <div class="one"> <button type="button" class="next-form" >Next</button> </div> <div class="two"> <button type="button" class="previous-form" >Previous</button> <button type="button" class="next-form" >Next</button></div> <div class="three"><button type="button" class="previous-form">Previous</button></div> </div> 

I fixed it by adding stop() 我通过添加stop()修复它

$(function() {
  $('.next-form').click(function() {

    current_fs = $(this).parent();
    next_fs = $(this).parent().next();

    $(current_fs).stop();
    $(next_fs).stop();

    $(current_fs).animate({
      opacity: 0
    }, "fast", function() {
      $(this).hide();
    });
    $(next_fs).css({
      position: 'absolute',
      left: '100%',
      top: 0
    });

    $(next_fs).show(function() {
      $(this).animate({
        left: 0
      })
    });

  });

  $('.previous-form').click(function() {

    current_fs = $(this).parent();
    previous_fs = $(this).parent().prev();

    $(current_fs).stop();
    $(next_fs).stop();

    $(current_fs).animate({
      left: '100%'
    }, function() {
      $(this).hide();
    });
    $(previous_fs).show(function() {
      $(this).animate({
        opacity: 100
      }, 2000);
    })

  });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM