I'm dynamically adding rows to a table using jQuery. The table
is inside a div
which has overflow:auto
thus causing a vertical scrollbar.
I now want to autoscroll my container div
to the last row. What's the jQuery version of tr.scrollintoView()
?
scrollIntoView(true);", element); This works for vertical scrolling but not for horizontal.
scroll() The scroll() method of the Element interface scrolls the element to a particular set of coordinates inside a given element.
To scroll to an element, just set the y-position to element. offsetTop . The SmoothScroll.
This following works better if you need to scroll to an arbitrary item in the list (rather than always to the bottom):
function scrollIntoView(element, container) { var containerTop = $(container).scrollTop(); var containerBottom = containerTop + $(container).height(); var elemTop = element.offsetTop; var elemBottom = elemTop + $(element).height(); if (elemTop < containerTop) { $(container).scrollTop(elemTop); } else if (elemBottom > containerBottom) { $(container).scrollTop(elemBottom - $(container).height()); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With