Scroll to bottom of div?
Here's what I use on my site:
var objDiv = document.getElementById("your_div");
objDiv.scrollTop = objDiv.scrollHeight;
How to make a Div scroll to the bottom
element.scrollIntoView(false);
If false
, the bottom of the element will be aligned to the bottom of the visible area of the scrollable ancestor.
MDN: Element.scrollIntoView()
Keep overflow div scrolled to bottom unless user scrolls up
This might help you:
var element = document.getElementById("yourDivID");
element.scrollTop = element.scrollHeight;
[EDIT], to match the comment...
function updateScroll(){
var element = document.getElementById("yourDivID");
element.scrollTop = element.scrollHeight;
}
whenever content is added, call the function updateScroll(), or set a timer:
//once a second
setInterval(updateScroll,1000);
if you want to update ONLY if the user didn't move:
var scrolled = false;
function updateScroll(){
if(!scrolled){
var element = document.getElementById("yourDivID");
element.scrollTop = element.scrollHeight;
}
}
$("#yourDivID").on('scroll', function(){
scrolled=true;
});
Detecting when user scrolls to bottom of div with React js
you can use el.getBoundingClientRect().bottom
to check if the bottom has been viewed
isBottom(el) {
return el.getBoundingClientRect().bottom <= window.innerHeight;
}
componentDidMount() {
document.addEventListener('scroll', this.trackScrolling);
}
componentWillUnmount() {
document.removeEventListener('scroll', this.trackScrolling);
}
trackScrolling = () => {
const wrappedElement = document.getElementById('header');
if (this.isBottom(wrappedElement)) {
console.log('header bottom reached');
document.removeEventListener('scroll', this.trackScrolling);
}
};
scroll to the bottom of the content if some content has been added to the `div`
If you use
msgdiv.scrollIntoView(false);
it will keep the scroll view at the bottom of the element. (true would keep it at the top.)
Scroll to bottom of Div on page load (jQuery)
The other solutions here don't actually work for divs with lots of content -- it "maxes out" scrolling down to the height of the div (instead of the height of the content of the div). So they'll work, unless you have more than double the div's height in content inside of it.
Here is the correct version:
$('#div1').scrollTop($('#div1')[0].scrollHeight);
or jQuery 1.6+ version:
var d = $('#div1');
d.scrollTop(d.prop("scrollHeight"));
Or animated:
$("#div1").animate({ scrollTop: $('#div1').prop("scrollHeight")}, 1000);
How to make a DIV scroll to its own bottom?
Use scrollTo
function on the div and scroll it to: scrollHeight
minus clientHeight
.
See MDN:
- clientHeight
- scrollHeight
- scrollTo()
Example: jsfiddle
Related Topics
Which "Href" Value Should I Use For JavaScript Links, "#" or "JavaScript:Void(0)"
How to Check File Mime Type With JavaScript Before Upload
Fastest Method to Escape HTML Tags as HTML Entities
Onchange Event Handler for Radio Button (Input Type="Radio") Doesn't Work as One Value
How to Loop Through Selected Elements With Document.Queryselectorall
How to Link a JavaScript File to a HTML File
Best Practice: Access Form Elements by HTML Id or Name Attribute
Stopping Gif Animation Programmatically
How to Prevent Buttons from Submitting Forms
How to Write Data to File Using Only JavaScript
Prevent Refresh of Page When Button Inside Form Clicked
Make Iframe Automatically Adjust Height According to the Contents Without Using Scrollbar
How to Render HTML String as Real Html
Chrome Extension - Retrieving Global Variable from Webpage