Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I scroll a row of a table into view (element.scrollintoView) using jQuery?

Tags:

jquery

scroll

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()?

like image 290
Fuxi Avatar asked Nov 26 '09 21:11

Fuxi


People also ask

Does scrollIntoView work horizontally?

scrollIntoView(true);", element); This works for vertical scrolling but not for horizontal.

How do you scroll with elements?

scroll() The scroll() method of the Element interface scrolls the element to a particular set of coordinates inside a given element.

How do I smooth scroll to Element?

To scroll to an element, just set the y-position to element. offsetTop . The SmoothScroll.


1 Answers

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());   } } 
like image 149
Abhijit Rao Avatar answered Sep 30 '22 10:09

Abhijit Rao