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.
hahah… that’s great code
Fantastic!!! i have to try it out. nice one
After checking the demon again, i noticed that, when i clicked on reply it goes down, but when i click on Prev OR Next, it doesn’t go back to d top!!! any bug?
ölüyorum seni tam buldum
my comment got eaten.
SEO means more than just Search Engine Optimization; it also means Start Engaging Others. But, why should you be engaging your employees, your customers, your potential customers, even your competition? To boost rankings, of course! Wielding Social Media as a tool of mass connectivity is the future of SEO.
How would I offset the scrolling for a fixed header, I don’t understand where to plug in the offset value.
I wrote a similar solution to be used with a Caspio online DataBase report page. It uses the scroll to plugin but moves automatically every few seconds. You can read about how it was put together here:
http://howto.caspio.com/?p=3759
Thanks
coool men cool… just like this one
http://bit.ly/demoscss
Superb
is not easy to put on weight
I’m trying to implement the script into my wordpress blog with no success. Any help?
Recent surveys, children of depressed mothers’ negative patterns of activity occurring in different brain reveals. This is for children of mothers who take more risks in the future is going to have depression.
Good Work, thx for the post!
Hi Guys.
I have been working on trying to find this script for the right project and one has come along finally but as per usual with most projects its not a straight forward scrollTo page.
They want the content hidden and then when the button is clicked it loads the content with ajax from an outside html page into the container div. I know this is possible as its been implemented via these guys http://www.imagemechanics.com.au.
Is there any easier cleaner way to do this at all?
Thanks
Mark
This has been highly helpful!
I have been looking for something like this for two weeks…
thanks!
Like the effect. However, if you’re on the comments or reply area and you try to click the previous link, it will not move to the previous post area.
This is verry good.
Goog job. Thanks.
How can I amend the js to work with a horizontal site? This is what I trying to get it to work on http://cargocollective.com/larryleathers