简体   繁体   English

jQuery只向下滚动DIV中的元素

[英]jQuery scroll down only the elements inside a DIV

There is a DIV in my website and it is the css 我的网站上有一个DIV ,它就是css

div{
max-height:400px;
overflow:auto;
}

I'm going to create a button to scroll down only the elements inside this DIV to a certain position not the whole page. 我将创建一个按钮,仅将此DIV内的元素向下滚动到某个位置而不是整个页面。 I mean I want to scroll down the DIV . 我的意思是我想向下滚动DIV I know how to do this for the whole page as this was asked before but I don't know how do this for a DIV . 我知道如何对整个页面执行此操作,因为之前已经提到过,但我不知道这对于DIV是怎么做的。

In plain vanilla Javascript you can simply do: 在简单的vanilla Javascript中,您可以简单地执行:

document.getElementById('yourDiv').scrollTop = *position*;

Where position is the scroll position you wish to set. 其中position是您要设置的滚动位置。

If you want to scroll the div to the top of another element within it, you can use eg: 如果要将div滚动到其中的另一个元素的顶部,可以使用例如:

document.getElementById('yourDiv').scrollTop = document.getElementById('scrollToElement').offsetTop;

If you want to use jQuery, you can nicely animate the scroll- using: 如果你想使用jQuery,你可以很好地使用滚动动画:

$("yourDiv").animate({scrollTop:*position*}, '500', 'swing', function() { 
   // anything to do after scroll
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM