Smooth page scroll to an anchor
Previous article Next articleWith jQuery you can easily create nice smooth page scrolls to different content blocks. See this demo!
How to use
This feature uses jQuery, so to start with you need to call the jQuery library in your template. If you already have it there because another module uses it, you don't need to add it twice!
Add this little jQuery code in your template:
<script>
jQuery(document).ready(function($) {
$(".scroll").click(function(event) {
event.preventDefault();
$('html,body').animate( { scrollTop:$(this.hash).offset().top } , 1000);
} );
} );
</script>
In your template or page you can use the anchors like this:
<ul>
<li><a href="#anchor1" class="scroll">Scroll to anchor 1</a></li>
<li><a href="#anchor2" class="scroll">Scroll to anchor 2</a></li>
</ul>
<a id="anchor1"></a>
<p>Lorem</p>
<a href="#top" class="scroll">Scroll back to top</a>
<a id="anchor2"></a>
<p>Ipsum</p>
<a href="#top" class="scroll">Scroll back to top</a>
Working example
Here a little demonstration:
#1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
#2. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
Comment Form
ReviewManager
ReviewManager
7 Comments
Thanks.
I changed my old script to this one.
Nice! thanks
Hi,
Thank you for the tutorial. It was really interesting.
But here is my question : how can we implement this in a cms made simple project ?
Thats great
Thank you very much.
Just very simple and GREAT
hahahah Great Man Its just Simple and awesome :) Thankx a lot (y)
Every thing is working beautifully with your example on all the browsers that I have tested but for some reason when I put it up on my site it dosent work on firefox and ie... have any idea why?