jquery滚动速度?

发布于 2024-10-10 15:13:10 字数 1262 浏览 4 评论 0原文

这是我从互联网上获得的一个脚本,它工作得很好,它在鼠标移动时自动滚动,在本例中是在 div 上滚动,但我似乎找不到我可以在哪里找到速度,或者可以让它变慢!我很困惑!

$("#scroll").mousemove(function(e){
        /* The scrollable quote container */

        if(!this.hideDiv)
        {
            /* These variables are initialised only the firts time the function is run: */

            this.hideDiv = $(this);
            this.scrollDiv = $('#scroll');

            this.pos = this.hideDiv.offset();
            this.pos.top+=20;
            /* Adding a 20px offset, so that the scrolling begins 20px from the top */


            this.slideHeight = this.scrollDiv.height();

            this.height = this.hideDiv.height();
            this.height-=20;
            /* Adding a bottom offset */

            this.totScroll = this.slideHeight-this.height;
        }

        this.scrollDiv.css({
            /* Remember that this.scrollDiv is a jQuery object, as initilised above */

            marginTop:'-'+this.totScroll*(Math.max(e.pageY-this.pos.top,0)/this.height)+'px'
            /* Assigning a negative top margin according to the position of the mouse cursor, passed
               with e.pageY; It is relative to the page, so we substract the position of the scroll container */
        });

    });

this is a script i got from a the internet, and it works perfectly, what it deos it scrolls automatically on the movement of the mouse, over a div in this case scroll, but i cnt seem to find where i can find the speed, or can make it slower!! im so confused!!

$("#scroll").mousemove(function(e){
        /* The scrollable quote container */

        if(!this.hideDiv)
        {
            /* These variables are initialised only the firts time the function is run: */

            this.hideDiv = $(this);
            this.scrollDiv = $('#scroll');

            this.pos = this.hideDiv.offset();
            this.pos.top+=20;
            /* Adding a 20px offset, so that the scrolling begins 20px from the top */


            this.slideHeight = this.scrollDiv.height();

            this.height = this.hideDiv.height();
            this.height-=20;
            /* Adding a bottom offset */

            this.totScroll = this.slideHeight-this.height;
        }

        this.scrollDiv.css({
            /* Remember that this.scrollDiv is a jQuery object, as initilised above */

            marginTop:'-'+this.totScroll*(Math.max(e.pageY-this.pos.top,0)/this.height)+'px'
            /* Assigning a negative top margin according to the position of the mouse cursor, passed
               with e.pageY; It is relative to the page, so we substract the position of the scroll container */
        });

    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

尬尬 2024-10-17 15:13:10

代码似乎只是直接设置边距:

marginTop:'-'+this.totScroll*(Math.max(e.pageY-this.pos.top,0)/this.height)+'px'

即,它不调用任何可以轻松制作动画的滚动 jquery 函数。要实现这一点,您必须重写该代码,可能使用 jquery animate() 函数和 marginTop css。

唯一的问题是该代码是在 mousemove 上调用的,这意味着在动画仍处于活动状态时可以轻松地再次调用该代码。因此,您必须想出一些解决方法,例如首先检查是否存在动画,然后在这种情况下中止它。

The code seems to be just setting the margin directly:

marginTop:'-'+this.totScroll*(Math.max(e.pageY-this.pos.top,0)/this.height)+'px'

That is, it doesn't call any scroll jquery function that could easily be animated. To achieve that you would have to do some rewriting of that code, probably using jquery animate() function with the marginTop css.

The only problem is that the code is called on mousemove, which means that it could easily be called again while the animation is still active. So you will have to come up with some workaround there, like maybe checking first if there is an animation present and aborting it in that case.

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