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.
a very good description…
thanks a lot
Thanks for sharing. I have been looking and asking for this.
it’s too cool idea thanx for sharing it post. it’s helpful post to me bcz i’m webdesigner. it’s awesome
thank for this post
love your site
very helpful! Thanks!
Very nice article, would You mind if I translate it to polish and publish it on my website :)?
Hello, it’s possible to hide the prev link and activate it after clicking the link next?
And add scrollTo normal but eliminate the link’s positions ?
Very nice article, would You mind if I translate it to polish and publish it on my website :)?
Great info. The provided demo is very useful.
Terrific
But left a “goto the begin’s” button
jquery css and gives you a power …. only have to use that for good ok
Just want to say what a great blog you got here!I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!
Can you please tell me how to implement this in blogger?
Easy to implement and another great tutorial.
Thanks for sharing. Easy to implement and also I will apply it.
Los Angeles Web Design
Its really understandable and good one.thanks for helping us
Excellent tutorial.
How would I offset the scrolling for a fixed header, I don’t understand where to plug in the offset value.
Any help would be much appreciated. Thank you.
thank u, now i can implement this to my blog,. really usefull for me…
As the Apple iphone 4 bumper is taken off the marke, will white iphone 4 available soon?
White iphone 4 avaiable! It’s a great news to tell you the latest white iphone 4 Conversion Kit is on sale. Get the chance to make your iphone more charming!