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

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

问题描述

每次用户靠近文档顶部时,我都会尝试向下滚动100px。

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天全站免登陆