2009
10.07

Today I had to make a function for a client of mine so I decided to share it with you.

It’s a oneliner but can add a really nice effect to your website.

This function needs jQuery

function goToByScroll(id){
     	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}

Of course you can change slow to normal or fast or even a int in milliseconds.

And to call it

<script>
goToByScroll("theIdIWantToGoTo");
</script>

Enjoy

  • Share/Bookmark

Related Posts:

18 comments so far

Add Your Comment
  1. This code saved me, I’ll send you a link to the site I’m working on when its finished.

  2. Glad to hear this ! Good luck

  3. ABSOLUTELY BRILLIANT!! I have been looking for this for a long time!! Thank you soooo much!!

  4. always share the code you write for your clients! :D

  5. thanks! at least i can understand this tutorial

  6. thanks.
    I avoid installing “scrollTo” jQuery plugin (which is good but too much for what I needed =)

  7. im so new with this…. where should i put this code? within the head tag?

  8. Works perfect!!!!!!!!!!! Thanx so much!!!!!!!!!

  9. Hej, thank you very much for this code, it is simple and easy to install. Just it doesn’t work well in Opera. When scroll to the top of the page it breaks.

  10. Thank you for this very compact and clean code.

    Question, if I want to scroll slower than ‘slow’ is there a solution?

    Thanks

  11. Works a treat…

    Unfortunately, it seems to be conflicting with lightbox.
    Can’t get them both to work on the same page.

    I was so close! :(

    Is there a fix?

    Thanx

    - Blaise

  12. Is there a way to do this within only a DIV element with an ID? I see the JS says “html,body” but what if I want to only scroll a section of my site within a Div tag??

  13. Thanks! This is just where i was looking for.
    And i think i’ll be running at the same problem with Blaise, anyone knows a solution.

    Keep up the good work.

  14. thanks, this is cool efect

  15. Great job!, I was looking for this solution for month and I dont know how i got to this blog.
    Thanks to share!

  16. god bless you,
    thanx so much

  17. DUde! i\’ve been working on using other jquery plugins for hours and i couldn\’t get it to work, then i found your site and in two min i have it. THANK YOU.

    Is there anyway to have the window slide to the div but with a marign?

    I\’m having my window slide to an image header but i would like there to be a 50px margin above the header rather than right on top of it.

    Please let me know!

  18. Thanks man!
    U very helped me..!!