mari
mari

Reputation: 229

scroll to a div

I am trying to use a very basic smooth scrolling feature with jQuery's scrollTop function, but no success. I want that any of the link which has css class "scroll" should always scroll down the page and stop at a div which has a class "stop". For example, I have HTML structure like:

<a href="#" class="scroll">Scroll down</a>
<div class = "test">long text</div>
<div class = "stop">Stop here</div>
<div class = "other">text</div>

Upvotes: 0

Views: 446

Answers (2)

rkw
rkw

Reputation: 7297

Try this:

$('a.scroll').click(function() {
    var stop = $(this).siblings('div.stop:first').offset().top;
    var delay = 2000;
    $('body').animate({scrollTop: stop}, delay);
});

Upvotes: 1

ShankarSangoli
ShankarSangoli

Reputation: 69905

Take a look at jQuery scrollTo plugin. It will help you make your life easier.

http://plugins.jquery.com/project/ScrollTo

Upvotes: 1

Related Questions