简体   繁体   中英

Scroll to a div contained in a overflow:auto

i have this:

<div id="parent" style="overflow:auto">
   ...
   <div id="test">Hello</div>
   ...
</div>

My question is: how to scroll parent div to have test div as first line of parent ?

If you don't want to use a plugin, see element.scrollIntoView :

$("#test").get(0).scrollIntoView();

// Plain ol' JS:
document.getElementById("test").scrollIntoView();

If you're using jQuery (or are able to do so), take a look at the ScrollTo plugin.

From documentation

With this plugin, you will easily scroll overflowed elements, and the screen itself. It gives you access to many different options to customize and various ways to specify where to scroll.

http://plugins.jquery.com/project/ScrollTo

The easiest way is to use this plugin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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