JQuery 变量在悬停时滚动。鼠标移开后怎样才能缓解?

发布于 2024-11-02 22:10:45 字数 1838 浏览 0 评论 0原文

好吧,我会尝试解释一下,但这有点困难。假设我有一些像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Oo萌小芽oO 2024-11-09 22:10:45

查看 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文