JQuery 变量在悬停时滚动。鼠标移开后怎样才能缓解?
好吧,我会尝试解释一下,但这有点困难。假设我有一些像这样的 html,
<div id="wrapper">
<div id="scroll">
<!--content-->
</div>
</div>
现在,#wrapper 有一个设定的高度,比如 500px,#scroll 更长的高度,比如 3000px。
var hoverInterval, yPos, offset, objHeight
$("#wrapper").mousemove(function(e){
//when the mouse moves in #wrapper, find the height of the object
//and the relative position of the mouse within the object.
objHeight = this.offsetHeight
yPos = e.pageY - this.offsetTop;
});
$("#wrapper").hover( //when the user hovers w/i #wrapper...
function(){
hoverInterval = setInterval(function(){
factor = (100*yPos)/objHeight //gives a range from 0-100
move = ( -3+( ( Math.pow(factor-50,2))/56))/8
/*this provides a bell curve, so that as
*the user hovers closer to the extremes,
*/#scroll will scroll faster up and down.
if ( move > 0 ) { //prevents movement when in the middle
if (yPos <= objHeight*.5 ) {
$("#photos").animate(
{"top":"+="+move+"px"},"slow","easeOutExpo")
.stop("true","true")
}
else if (yPos >= objHeight*.5){
$("#photos").animate(
{"top": "-="+move+"px"},"slow","easeOutExpo")
.stop("true","true")
}
}
},10); //setInterval's timeout
},
function(){ //and stop at mouse off.
clearInterval(hoverInterval)
}
);
现在的作用是,当用户将鼠标悬停在 #wrapper 中更高或更低时, #scroll 滚动速度更快,中间有死区。但是当用户滚动离开 #wrapper 时,它会突然停止。关于当用户停止将鼠标悬停在 #wrapper 上时如何优雅地停止此操作的任何想法?
也欢迎对我的代码进行优化。
Ok, I'm going to try to explain this, but it's kind of difficult. Say I have some html like this,
<div id="wrapper">
<div id="scroll">
<!--content-->
</div>
</div>
Now, #wrapper has a set height, say 500px, and #scroll a longer height, say 3000px.
var hoverInterval, yPos, offset, objHeight
$("#wrapper").mousemove(function(e){
//when the mouse moves in #wrapper, find the height of the object
//and the relative position of the mouse within the object.
objHeight = this.offsetHeight
yPos = e.pageY - this.offsetTop;
});
$("#wrapper").hover( //when the user hovers w/i #wrapper...
function(){
hoverInterval = setInterval(function(){
factor = (100*yPos)/objHeight //gives a range from 0-100
move = ( -3+( ( Math.pow(factor-50,2))/56))/8
/*this provides a bell curve, so that as
*the user hovers closer to the extremes,
*/#scroll will scroll faster up and down.
if ( move > 0 ) { //prevents movement when in the middle
if (yPos <= objHeight*.5 ) {
$("#photos").animate(
{"top":"+="+move+"px"},"slow","easeOutExpo")
.stop("true","true")
}
else if (yPos >= objHeight*.5){
$("#photos").animate(
{"top": "-="+move+"px"},"slow","easeOutExpo")
.stop("true","true")
}
}
},10); //setInterval's timeout
},
function(){ //and stop at mouse off.
clearInterval(hoverInterval)
}
);
What this does now is, when the user hovers higher or lower in #wrapper, #scroll scrolls faster, with a dead area in the middle. But when the user scrolls off of #wrapper, it stops suddenly. Any ideas on how I can make this gracefully stop when the user stops hovering over #wrapper?
Optimizations to my code are also welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 jQuery 事件
mouseout
。我建议您将这样的事件附加到包装器,并使用您已经设置的速度变量从那里平滑地停止动画。http://api.jquery.com/mouseout/
Check out the jQuery event
mouseout
. I suggest you attach such an event to the wrapper and stop the animation smoothly from there using the speed variables you already have set.http://api.jquery.com/mouseout/