views:

57

answers:

5

If you click on the navigation links on Hardly Code's website the browser scrolls to another section of the page.

Can anyone point me to some good reading materials for recreating this effect? I am using jquery, and want the animation to be as smooth as is it on hardlycode.com

+1  A: 

Use the ScrollTo jQuery plugin: http://flesler.blogspot.com/2007/10/jqueryscrollto.html - http://plugins.jquery.com/project/ScrollTo

CD Sanchez
thanks, it's a very smooth animation, I love the fact that it can handle sideways movement too - perfect if you're targeting handhelds
stephenmurdoch
+3  A: 

The command is

$('html,body').animate({scrollTop : 0},'slow');

Or, if the link has class scrolltop_btn

$('.scrolltop_btn').click(function() {
  $('html,body').animate({scrollTop : 0},'slow');
});

as a catch effect, in case javascript is disabled, you should always include the standard HTML anchors too.

If you want to change the duration, you can change 'slow' to the number of milliseconds you want the effect to last.

Kranu
thanks, I think this is the best option as it doesn't require a plugin or extra library...
stephenmurdoch
Yep, using a plugin for jQuery to scroll is over-complicating something that can already be done in one or two lines.
Kranu
+1  A: 

Here's a blog post on doing it with jQuery: Improved Animated Scrolling Script for Same-Page Links

You can do just scrolling to top, but that method lets you point it to any section of the page exactly the same as if you were using #anchors. It's a great example of progressive enhancement, since the fancy Javascript effect is in addition to an already completely functional no-Javascript system.

Matchu
The page he linked actually uses the first snippet from that blog post.
CD Sanchez
@CD Sanchez: ah, I've got good Googling skills, then. The site in question used this blog post. Neat! :)
Matchu
thanks this is a very helpful article :)
stephenmurdoch
+1  A: 

Here a good one:

http://flesler.blogspot.com/2007/10/jqueryscrollto.html

Try the demo first:

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

Good luck!

Trufa
nice link, the scrolling is quite smooth. i like it.
stephenmurdoch
@stephenmurdoch Glad you liked it! hope it helps!
Trufa
+1  A: 

A very simple one which requires no in-page javascript (it simply finds all links which contain an anchor and makes them scroll instead): http://www.kryogenix.org/code/browser/smoothscroll/

TRiG
thanks for the link.
stephenmurdoch