-1

What I'm trying to do is very simple, but i can't find any ways to do it (and i've searched!)

I want that, when the page is loaded, the page scroll to an element with a class="" in the page. I've seen that there are a lot of plugins, and i've tried thing like this :

     $('html, body').animate({
         scrollTop: $(".class").offset().top
     }, 2000);

but it doesn't work :(

estellechvl
  • 335
  • 4
  • 14

2 Answers2

3

Try like this:

$(document).ready(function () {
    // Handler for .ready() called.
    $('html, body').animate({
        scrollTop: $('#what').offset().top
    }, 'slow');
});

FIDDLE DEMO

palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • what's the difference between this and what i've already tried ? :/ – estellechvl May 30 '13 at 13:22
  • @Dellwolf the difference is that he's calling the `animate scrollTop` when the document is ready. If the answer he gave helped you to solve your question, please mark as answer. Thanks. – Pedro Estrada May 30 '13 at 13:48
  • i already know that i have to mark if it helped, Thanks Pedro. anyway, yes the fiddle works Palash, but in my code, i did call the animate after the document is ready, but it still doesn't work. Ok, the thing is that i have a lot of functions, the problem must be coming from my code. I'll try to find where it comes from. Thank you! – estellechvl May 30 '13 at 13:57
0

You might also want to look at scrollTo(). It's a plugin and it works nicely, lets you set scrolls to any element, percent of page, anchors. Also can setup multiple scrolls, scrolls inside child elements fairly easily.

Link: http://flesler.com/jquery/scrollTo/

TheSnooker
  • 935
  • 2
  • 11
  • 24