Inspired by the CargoCollective and David DeSandro's site, I asked my Twitter followers (@nickla) on how to do the scrollto posts with jQuery. Within a day, Ben Bodien of Neutron Creations sent back a quick demo on how to replicate the similar result with the ScrollTo plugin. The script finds your current view position and scroll to the next or previous post accordingly. Check out the demo to see what I'm talking about.
HTML Code
Assume you are using this for your blog and each blog entry has a "post" CSS class.
<div class="post">
content...
</div>
<div class="post">
content...
</div>
<div class="post">
content...
</div>
CSS: Navigation Dock
Specify position:fixed for the navigation div to prevent it from scrolling.
#nav-dock {
position: fixed;
right: 15px;
top: 35%;
}
Javascript
Include the following code in the <head> tag. The first set of function will check which post you are currently viewing. When the next or prev button is clicked, it will scroll to the post accordingly.
The second function is to handle anything that's got a class of "scrolltoanchor". It will scroll to the element with an ID corresponding to that element's href attribute value. In my case, I use it for the Back to top, Comments, and Reply button.
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery.scrollTo-min.js"></script>
<script type="text/javascript">
$(function() {
function scroll(direction) {
var scroll, i,
positions = [],
here = $(window).scrollTop(),
collection = $('.post');
collection.each(function() {
positions.push(parseInt($(this).offset()['top'],10));
});
for(i = 0; i < positions.length; i++) {
if (direction == 'next' && positions[i] > here) { scroll = collection.get(i); break; }
if (direction == 'prev' && i > 0 && positions[i] >= here) { scroll = collection.get(i-1); break; }
}
if (scroll) {
$.scrollTo(scroll, {
duration: 750
});
}
return false;
}
$("#next,#prev").click(function() {
return scroll($(this).attr('id'));
});
$(".scrolltoanchor").click(function() {
$.scrollTo($($(this).attr("href")), {
duration: 750
});
return false;
});
});
</script>
Credits
Thanks to Neutron Creations for the ScrollTo implementation.
Beauty! Been looking just for this one! Thanks heaps, gracias senor :)
Nice Article.
Nice article buddy.
Thanks for sharing. I have two questions, how would one attach a keyboard shortcut to this so that we could achieve J/K navigation between posts (like in ffffound)? and, could we set a variable so that we can easily change the effect’s duration?
Again, thank you very much!
nice info..
good tutorial..
Hi Thanks for this. I’m tryig to get thisi working localy, by copying the code and linked files, but no joy. Anything I’m missing? Thanks
Appologies, forget last post. Works fine. User error
Nice article, I’ll implement this on my blog.
Thanks for sharing this tutorial mate. But i have question. Can i make this work with my thesis? Bcoz i need hooks for implementing this in thesis. Help me
Thanks for sharing this article …
Great, I may have to consider this for my site. Thanks for sharing
really nice thx
Could be useful, thanks !
I have used nearly the same technique within my website, seems to be the “omg hawtest 2.0 trend” it’s nifty, definitely a lot better than just jumping to anchor links. Good post though Nick.
OMG I’ve been looking for something like this for a while. I noticed Windows Live used a similiar effect on their Hotmail preview page (http://windowslivepreview.com/hotmail/new/) a long time ago.. and I was wondering if anyone knew how to extend this function to make it work like Windows Live did. As you’ll see as you scroll down the page, the navigations links are highlighted as you scroll by the headings. I hope someone can help :)
Nick, you should definitely post mode “jQuery” related posts. Since it is realy “must have” feature in these days :) Thanks for this one
Nice! Thanks for sharing…
Great list, thanks for sharing it! Here’s a new free blog directory that you can also add to your list:
http://www.webmediamakers.com
Really nice! Thanks for sharing.
good teacher.:D