简体   繁体   English

jQuery动画速度问题

[英]JQuery animate speed issue

I have a div which I want to show on scroll and hide when user scroll back, its working but not as expected. 我有一个div ,我想在滚动时显示,并在用户向后滚动时隐藏,它可以正常工作,但不符合预期。 The problem is when I scroll down then it appears fine, but when I scroll back there is a delay in hide. 问题是当我向下滚动时它看起来很好,但是当我向下滚动时,隐藏时有延迟。

What I want is show div with slide from top to bottom, and when hiding bottom to top effect. 我想要的是从顶部到底部显示div并隐藏底部到顶部的效果。

Here is the code I have been trying: 这是我一直在尝试的代码:

 $(window).scroll(function() { var fheader = $(".top-header"); if ($(this).scrollTop()>50) { $(fheader).animate({top: "0px"},{duration: 100, easing: "linear"}); } else { $(fheader).animate({top: "-50px"},{duration: 100, easing: "linear"}); } }); 
 .top-header { width: 100%; position: fixed; height: 50px; background: #fff; border-bottom: 2px solid #ccc; top: -50px; left: 0; } .content { width: 100%; height: 1400px; float: left; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="top-header"> header </div> <div class="content"> this is content </div> 

Just add stop() before animate to prevent queue exécution : 只需在animate前添加stop()即可防止队列执行:

Here is a working example : 这是一个工作示例:

 $(window).scroll(function() { var fheader = $(".top-header"); if ($(this).scrollTop()>50) { $(fheader).stop().animate({top: "0px"},{duration: 100, easing: "linear"}); } else { $(fheader).stop().animate({top: "-50px"},{duration: 100, easing: "linear"}); } }); 
 .top-header { width: 100%; position: fixed; height: 50px; background: #fff; border-bottom: 2px solid #ccc; top: -50px; left: 0; } .content { width: 100%; height: 1400px; float: left; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="top-header"> header </div> <div class="content"> this is content </div> 

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

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