简体   繁体   English

如何在溢出的div上设置x和y滚动position:滚动?

[英]How to set x and y scroll position on a div with overflow: scroll?

If I have the following markup:-如果我有以下标记:-

 .outerDiv { width: 500px; overflow: scroll; }.innerDiv { width: 1000px; }
 <div class="outerDiv"> <div class="innerDiv"> </div> </div>

How do I (most likely with JavaScript) set the x and y position of that scroll?我如何(最有可能使用 JavaScript)设置该滚动的 x 和 y position?

You can use scrollLeft and scrollTop properties. 您可以使用scrollLeftscrollTop属性。 Ex: 例如:

document.getElementById("yourScrollElementId").scrollTop = 100

Or you can use jquery methods to make it easier and animated as well. 或者您可以使用jquery方法使其更容易和动画。

You can use jQuery scrollLeft() and scrollTop() methods. 您可以使用jQuery scrollLeft()和scrollTop()方法。

$('.outerDiv').scrollLeft(300);
$('.outerDiv').scrollTop(50);

Example: https://jsfiddle.net/upwrrj70/ 示例: https//jsfiddle.net/upwrrj70/

A working example:一个工作示例:

 document.getElementById('mySection').scrollTop = 50
 section { border: 1px solid gray; overflow-y: scroll; max-height: 50px; }
 <section id="mySection"> <div>foo 1</div> <div>foo 2</div> <div>foo 3</div> <div>foo 4</div> <div>foo 5</div> <div>foo 6</div> <div>foo 7</div> <div>foo 8</div> <div>foo 9</div> </section>

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

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