Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I scroll to first visible element with a specific div class?

Is there anyway to automatically scroll to the first visible element with a certain div class using javascript?

thanks!

like image 590
user962642 Avatar asked Sep 25 '12 21:09

user962642


People also ask

How do I scroll to a specific div in HTML?

The scrollIntoView method: The scrollIntoView() is used to scroll to the specified element in the browser. Example: Using scrollIntoView() to scroll to an element.

How do I automatically scroll a div?

How do you make a div scrollable in HTML? For a scrollable bar, use the x and y-axis. Set the overflow-x: hidden; and overflow-y: auto; to automatically hide the horizontal scrollbar and show a vertical scrollbar.

How do I scroll down to a specific element in Javascript?

Scrolling to an element can be achieved in Javascript using the scrollIntoView() method. Smooth animation and customizing the alignment can be set through the scrollIntoViewOptions parameter.


1 Answers

You should be able to use something like this:

$('html, body').animate({
    scrollTop: $('.class:visible:first').offset().top
}, 1000);

Demo: http://jsfiddle.net/Blender/xUw54/2/

like image 200
Blender Avatar answered Nov 02 '22 13:11

Blender