如何使用 JQuery $.scrollTo() 函数滚动窗口 [英] How to scroll the window using JQuery $.scrollTo() function

查看:26
本文介绍了如何使用 JQuery $.scrollTo() 函数滚动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户靠近文档顶部时,我都会尝试向下滚动 100 像素.

I'm trying to scroll down 100px every time the user gets near the top of the document.

我在用户接近文档顶部时执行了该函数,但 .scrollTo 函数不起作用.

I have the function executing when the user gets close to the top of the document, but the .scrollTo function isn't working.

我在前后放置了一个警报,以检查是否真的是线路阻止了它,只有第一个警报响起,代码如下:

I put an alert after and before to check to see if it actually was the line or not that was stopping it and only the first alert goes off, here's the code:

alert("starting");
$.scrollTo({ top: '+=100px', left: '+=0px' }, 800);
alert("finished");

我知道我已经正确链接了 jquery 页面,因为我在整个过程中使用了许多其他 jquery 函数,并且它们都可以正常工作.我也试过从上面删除px",但似乎没有什么区别.

I know I have the jquery page linked properly because I'm using many other jquery functions throughout and they all work fine. I've also tried removing the 'px' from above and it doesn't seem to make a difference.

推荐答案

如果它不起作用,为什么不尝试使用 jQuery 的 scrollTop 方法?

If it's not working why don't you try using jQuery's scrollTop method?

$("#id").scrollTop($("#id").scrollTop() + 100);

如果您希望平滑滚动,可以使用基本的 javascript setTimeout/setInterval 函数使其在设定的时间长度内以 1px 为增量滚动.

If you're looking to scroll smoothly you could use basic javascript setTimeout/setInterval function to make it scroll in increments of 1px over a set length of time.

这篇关于如何使用 JQuery $.scrollTo() 函数滚动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆