使用jquery setTimeout & 滚动div鼠标输入&鼠标离开
我有一个 div 容器,它粘在页面底部。当鼠标移出div时,我希望div在3秒后下沉。当鼠标移到 div 上时,我希望 div 上升到其原始位置。问题是,当鼠标快速移出 div 时,div 会不断向上移向页面顶部。
var timer = null;
var moving_distance = $("#scroller").height()-($(window).height()-$("#slideshow").height());
$("#scroller").mouseenter(function(event){
if(timer)
{
clearTimeout(timer);
$("#scroller").animate({top:'-='+moving_distance},1000);
}
}).mouseleave(function(event){
if(!timer){
timer = setTimeout(function(){
$("#scroller").animate({top:'+='+moving_distance},1000);
},3000);
}
});
I have a div container which sticks to the bottom of the page. When the mouse moves out of the div, I want the div to sink after 3 seconds. When the mouse moves over the div, I want the div to rise to its original position. The problem is when the mouse moves over and out of the div very quickly, the div keeps moving up towards the top of the page.
var timer = null;
var moving_distance = $("#scroller").height()-($(window).height()-$("#slideshow").height());
$("#scroller").mouseenter(function(event){
if(timer)
{
clearTimeout(timer);
$("#scroller").animate({top:'-='+moving_distance},1000);
}
}).mouseleave(function(event){
if(!timer){
timer = setTimeout(function(){
$("#scroller").animate({top:'+='+moving_distance},1000);
},3000);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经成功地将空动画堆叠在我想要的动画前面,而不是使用 setTimeout。
例如,
我从您的描述中猜测您希望 div 保持静止,直到鼠标移动到 div 中。然后,当老鼠第一次离开时,它应该下沉。之后,当鼠标再次返回时,它应该返回到原来的位置。我认为这将接近实现这一目标。
华泰
I have had success stacking a null animation in front of the animation I want rather than using setTimeout.
For instance,
I am guessing from your description that you want the div to stand still until the mouse is moved into the div. Then when the mouse leaves for the first time it should sink. After that, when the mouse comes back again, it should return to its original position. I think this will be close to doing that.
HTH