Scroll window smoothly in jQuery – Animated scroll

Using jQuery animate() function to do a smooth scrolling:

I have also created a div as follow:

<div id="scrollToHere">
Scroll to here
</div>

You need something to run your script. Create a button like this:

<input type="button" onclick="scollWin();" value="Scroll up" />

The jQuery Code will be like this:

function scrollWin(){
$('html,body').animate({
scrollTop: $("#scrollToHere").offset().top
}, 2000);
}

Here is something to try:

now jQuery window scrolling is done with animation.

Enjoy it..

+119
-28
  

61 Comments to “Scroll window smoothly in jQuery – Animated scroll”

  1. admin 13 May 2009 at 11:57 am #

    The down scroll is working fine. But the UP scroll is not. It jumps up without animation.
    All you have to do is remove the (,body) from $(“html, body”)

    Because Opera supports both. So, it scrolls twice.
    Let the code be: $(“html”).animate…..

    On safari, firefox and IEs: this code works fine.

  2. Paul Radich 14 June 2009 at 7:02 am #

    Nice Thanks!

  3. ZK@Web Marketing Blog 21 June 2009 at 6:46 am #

    Just started using jQuery here a few months ago, love it! Can’t wait to read more from this blog. Know of any good jQuery Blog Aggregators?

  4. Marlon 25 June 2009 at 6:59 am #

    Cool, thanks!

  5. Tyler 30 June 2009 at 4:49 am #

    Thanks! Just wanted to point out a small typo, the onclick should of course read “scrollWin();” but an r was left out. A very small error, but it does break the script, so wanted to point it out in case anyone was having trouble.

  6. admin 4 August 2009 at 9:34 am #

    Thanks Shick for your comment.

    This is the first time I hear about flashing screen when trying this code.

    Actually,
    This code was tested on The following browsers:
    - IE 6 and 7 (window)
    - Firefox (windows and mac)
    - Opera (windows and mac)
    - Safari (mac)

    I did not experience any problem with this code.
    Please provide with all information about the jQuery version and your browser version.

  7. Shick 6 August 2009 at 12:55 am #

    Thank you so much!!!

    I used the javascript fix, and it worked like a charm. thank you so much!!!!

  8. Anz 16 August 2009 at 4:12 pm #

    Click 2 or 3 times in the scroll link and try using your mouse scroll wheel!!

  9. admin 17 August 2009 at 9:30 am #

    The scroll animation keeps going. So, if you try to scroll the window using mouse wheel after clicking on the button, it will surely continue till the animation is done.

    If you want to stop the animation in any action taken, then try to use $(“html,body”).stop();
    this will stop all animations on window.

  10. Evan 20 August 2009 at 4:17 am #

    is there a simple fix for this to scroll horizontally, to the left?

  11. steven 18 October 2009 at 7:07 am #

    Not working on ie8

  12. admin 18 October 2009 at 9:51 am #

    Actually, It works fine on IE8

    I tested it on IE 8 (Windows XP) and IE 8 (Windows 7). It works fine.

  13. ben 2 November 2009 at 9:15 pm #

    so yeah thanks a lot.

  14. phil 13 November 2009 at 7:26 pm #

    mispelling: “scollWin();”

  15. admin 17 November 2009 at 12:30 am #

    Thanks for the suggestion Hiperblade.

    I think i missed that out ;)

  16. M. 1 December 2009 at 11:40 pm #

    When the site is scrolled to the top, the button “Scroll up” works by scrolling down… FF3.5

  17. El Oscuro 2 December 2009 at 4:41 pm #

    Amazing article, thank you!

    Helped me a lot.

  18. Martin 8 December 2009 at 12:37 am #

    Works fine. Thank you for helpfully article.

  19. Natalie 13 December 2009 at 2:27 am #

    Is there any way this can be used horizontally? Either to a div or using pixel locations?

  20. admin 13 December 2009 at 10:09 am #

    Actually, there a Javascript code for that

    window.scrollBy(x,y);
    // X for horizontal and Y for vertical.

    So, you can scroll window in both ways.

  21. Andres 29 December 2009 at 7:37 am #

    Works fine, tks

  22. 奇流网 12 January 2010 at 1:23 pm #

    is very good

  23. Syd 23 January 2010 at 11:21 pm #

    Good one.

  24. Preetham 9 February 2010 at 7:19 pm #

    simple and efficient,, Thanks

  25. Robin 10 February 2010 at 4:38 am #

    Thanks, Was really helpful!

  26. poupouille 3 March 2010 at 11:18 am #

    hi

    Thanks for this simple, usefull, and reusable tip

    have a nice day

  27. webhostph 13 March 2010 at 11:50 pm #

    what if i am using it to have in several locations?

    div id=1
    div id=2
    div id=3
    div id=4

    scroll to div 1
    scroll to div 2
    scroll to div 3
    scroll to div 4

    how can we do this? Its like this: at the bottom there are anchor tags that will need to scroll up/down on the specific div id. check the site i am working on to see what i meant, http://www.bryanvenancio.com

    Thanks!

  28. Angga Lingga 22 March 2010 at 11:01 am #

    Thank you very much … It works …

  29. remus 27 March 2010 at 10:37 pm #

    nice examples

  30. Dave 15 April 2010 at 8:16 pm #

    Great info, and very helpful. One question from a jQuery newbie – how would I add easing to this? Thanks for any help you can give!

  31. admin 15 April 2010 at 8:41 pm #

    To get the jQuery easing, see this:
    http://gsgd.co.uk/sandbox/jquery/easing/

  32. Milan Adamovsky 27 April 2010 at 11:34 pm #

    Nice plugin.

  33. moncler 4 June 2010 at 12:10 pm #

    nice post

  34. chok 10 June 2010 at 4:01 pm #

    Thanks a lot ! It work without bug in all browsers.

  35. payday loans 14 June 2010 at 2:50 pm #

    This is a superb post Freelancer ID Blog .
    But I was wondering how do I suscribe to the RSS feed?

  36. samuel jesse 25 June 2010 at 2:47 pm #

    thanks for this post, its by far the clearest and simplest way of using jquery to scroll I have found – the user comments helped with exactly what I needed as well!

  37. zulfiqar ali 26 June 2010 at 10:40 am #

    de thing which i wants.

    Cool Dude thanks

  38. Air Jordan shoes 26 June 2010 at 12:27 pm #

    Wonderful post. It is really useful to scan your blog. This article connected with your site is exactly splendid, and your blog design is Simple generous. It seems not dazzled. So excellent.

  39. Anton 28 June 2010 at 2:27 pm #

    de thing which i wants.

    Cool Dude thanks

  40. chad 29 June 2010 at 5:19 pm #

    Great post! I am just starting out in community management/marketing media and trying to learn how to do it well – resources like this article are incredibly helpful.

  41. the richjanitor 4 July 2010 at 11:31 am #

    Hmm, your site took quite a long time to load but it was worth it

  42. Blogging To The Bank 10 July 2010 at 9:08 pm #

    I wanted to thank you for this great read!! I definitely enjoying every little bit of it.I have you bookmarked to check out new stuff you post.

  43. Dachy Blachy 3 August 2010 at 3:40 pm #

    Wow, this is a great article. I will browse that blog some more for the fine reading.

  44. Coloring Pages 5 August 2010 at 10:13 pm #

    A topic close to my heart thanks. Like your blog design too.

  45. 传世私服 12 August 2010 at 7:51 pm #

    You have mention good post above I really enjoy the information. I wish to come again on your site in future

  46. john 13 August 2010 at 4:29 pm #

    Thanks for uploading such a nice scroller. Working fine everywhere……. :-)

  47. James 20 August 2010 at 5:14 pm #

    THANK YOU!

  48. very pleasuable 23 August 2010 at 5:52 pm #

    very interesting post – please do it again

  49. yes please 24 August 2010 at 11:01 pm #

    i love reading your website

  50. lovely 27 August 2010 at 6:05 pm #

    please supply me your rss feed

  51. i like it 28 August 2010 at 3:27 pm #

    yes i like it very much

  52. Ravi Tamada 8 October 2010 at 10:12 am #

    Thanx mate its very easy no need of other jQuery plugins …..

  53. Mehedi 16 October 2010 at 8:57 am #

    thanks for this useful post

  54. Carroll Valen 10 January 2011 at 11:07 pm #

    Great article. Waiting for more.

  55. Snoring Help 7 July 2011 at 12:48 am #

    Another excellent article! Thank you!

  56. Javier 13 September 2011 at 3:29 am #

    Yeahh you are the best!! Thanks for the script.

  57. matraci 11 October 2011 at 12:28 am #

    good work

  58. WHS 11 October 2011 at 12:30 am #

    good work

  59. MST 24 October 2011 at 11:24 pm #

    Thanks a lot !

  60. fenix 29 November 2011 at 12:02 pm #

    Thanks a lot

  61. memo666 24 December 2011 at 4:46 am #

    Great! Works fine.
    Thanks a lot.


Leave a Reply