amateur
amateur

Reputation: 44635

scroll to a set of elements on screen with jquery

I want to write a jquery function that will take a number of div elements by id or class and the screen scroll so that they are in view. I know that this may not be possible if the divs are spread over the page and not possible to get all in view, but would like to be able to get some in view for the user to be aware off.

Any tips on how I can do this?

Upvotes: 1

Views: 2536

Answers (3)

tomaszkubacki
tomaszkubacki

Reputation: 3262

without jquery plugin $(window).scrollTop($("#destinationDiv").offset().top );

Upvotes: 0

dropson
dropson

Reputation: 1121

If you want to scroll the window to an element, you could use the scrollTo plugin for jQuery.

$.scrollTo('#focusOnMe');

You can visit this page to view some demos of the plugin at work.

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630559

If you have a selector you can just use .scrollTop() to go to the first element the selector matches, like this:

$(window).scrollTop($(".selector").scrollTop());

Or to animate it so it's not an immediate jump there, use .animate():

$("html, body").animate({ scrollTop: $(".selector").scrollTop() }, 5000);

Upvotes: 5

Related Questions