想要滚动一个DIV元素,而不是整个网页

3
长话短说,我需要找到一种快速的方法从Google Chrome的JavaScript控制台滚动一个div元素。这是我试图滚动的元素的图片:http://i1250.photobucket.com/albums/hh527/dr4g1116/Capture_zps63402209.png。下面是包含DIV项的HTML,我需要滚动它(在顶部突出显示):http://i1250.photobucket.com/albums/hh527/dr4g1116/Capture1_zps04c66647.png。以下是我迄今为止尝试过的内容:首先:
<NestedList_trends_trends_variable_selector_variable_selector_variable.scrollTo>(0,250)

接下来:

NestedList_trends_trends_variable_selector_variable_selector_variable.scrollTo(0,250) 

最后:

$('body').animate({ scrollTop: $('#NestedList_trends_trends_variable_selector_variable_selector_variable').offset().top}, 5000)

这最后一个方法只是滚动了整个页面。不确定为什么,因为我引用了正确的DIV ID。
我尝试调整这三种方法之一,但都无济于事。我已经没有想法了,在网上找到的东西都没有帮助我。请问有人知道如何滚动此元素吗?请告诉我!
谢谢和祝贺!
更新:
尝试了这个方法,但没有效果:
$('#NestedList_trends_trends_variable_selector_variable_selector_variable').animate({ scrollTop: $('#Button_86').offset().top}, 5000)

你是不是有奇怪的“id选择综合症”? - iConnor
尝试使用 $('html,body').animate({scrollTop:$(".NestedList")。offset()。top},5000); 进行平滑滚动到一个元素,引用自http://www.abeautifulsite.net/blog/2010/01/smoothly-scroll-to-an-element-without-a-jquery-plugin/。 - Dom Day
@Connor - 不,这是遗留软件,我正在为它们编写测试。我不能更改ID。 - dr4g1116
@DomDay - 非常感谢您的建议。虽然它没有起作用,但我可以尝试像操作其他代码一样操纵它。 - dr4g1116
2个回答

0

看起来你已经有了jQuery,所以这应该可以:

$("#NestedList_trends_trends_variable_selector_variable_selector_variable").scrollTop(250);

或者让我的大脑停止疼痛:

//somewhere at top of file or in a bloody config somewhere
var bigAssIdDiv = $("#NestedList_trends_trends_variable_selector_variable_selector_variable");
...
///later
bigAssIdDiv.scrollTop(250);

请注意,使用scrollTop时,您提供的数字是隐藏在顶部上方的像素数(类似于向下滚动该像素数...)

0

你正在给body元素添加动画效果,需要给目标div添加动画。此外,我建议缩短该元素的ID或使用其中一个类来定位该元素,使其更易管理。无论哪种方式,尝试使用上一个代码片段中的代码,将嵌套列表放在第一个选择器内,并将要滚动到的子元素放在scrollTop属性的选择器内。


尝试了那个,但仍然不起作用。感谢您的建议。请查看我的更新,了解我是如何做到的。也许我做错了什么? - dr4g1116

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接