简体   繁体   中英

How to scroll to the farest right side of an overflowing div box?

the title is quite verbose of what I want to achieve,

I have a div box which displays a scroll on narrow screens (overflow-x). Since that box contains a filesystem breadcrumbs I want to automatically scroll to the right side of that box as to improve the user experience as the user can quickly peep where he stands in the filesystem tree.

How can I do that using only native apis? no jquery or else.

You can get the maximum scroll width by using scrollWidth , then, just set the value of the div's scrollLeft to the max width:

 function scroll_it(){ the_box = document.getElementById("box"); the_box.scrollLeft = the_box.scrollWidth; }
 #box {width:100px; height:100px; overflow-x:scroll; background:red;}
 <div id="box"> ABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFG </div> <a href="#" onclick="scroll_it()">Click me to scroll</a>

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