JavaScript scrollTo方法什么也不做? [英] JavaScript scrollTo method does nothing?

查看:94
本文介绍了JavaScript scrollTo方法什么也不做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是desperatley试图让一些滚动功能在一个页面上工作。在没有运气之后,我决定在我的页面上粘贴 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。然后我使用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屋!

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