JavaScript scrollTo 方法什么都不做? [英] JavaScript scrollTo method does nothing?
问题描述
所以我绝望地试图获得一些滚动功能来在页面上工作.运气不好后,我决定将 window.scrollTo(0, 800);
粘贴在我的页面上,看看是否可以进行任何滚动.什么都不会发生.我有一个可以展开的手风琴,然后我想滚动到其中的特定元素.但是现在我很高兴看到这东西滚动.有人遇到过这种情况吗?
So I am desperatley trying to get some scrolling functionality to work on a page. After not having a luck I decide to just stick window.scrollTo(0, 800);
on my page to see if I could get any scrolling to happen. Nothing does happen. I have an accordion that expands and then I want to scroll to a specific element with in it. But for now I would be happy to just see the thing scroll at all. Anyone run into this?
谢谢!
推荐答案
我能够使用 jQuery 方法 animate() 解决这个问题.这是我采用的实施示例:
I was able to resolve this problem using jQuery method animate(). Here is an example of the implementation I went with:
$('#content').animate({ scrollTop: elementOffset }, 200);
选择器正在获取 ID = "content" 的 div.然后我将 animate 方法应用于其上,并使用 scrollTop 作为选项.第二个参数是以毫秒为单位的动画持续时间.我希望这对其他人有帮助.
The selector is getting the div with ID = "content". I am then applying the animate method on it with scrollTop as an option. The second parameter is the time in milliseconds for the animation duration. I hope this helps someone else.
这篇关于JavaScript scrollTo 方法什么都不做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!