Smooth page scroll to an anchor

  Previous article Next article  

Smooth page scroll to an anchor

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.2
  Last updated: 25-12-2015
  Comments: 6

With 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:

{cms_jquery}

<script type="text/javascript">
  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:

<a id="top"></a>

<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.

Scroll back to start demo

#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?

Scroll back to start demo

Scroll to page top ^


A2 Webhosting CMS Made Simple

  Comments

  Click here to leave a comment






This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
  « Enter the CAPTCHA code (lower case)

Your IP address will be stored on submit. I reserve the right to change or delete your response without notice!
In case of abuse your IP address will be blocked!


Can I have your attention for a moment?

Did this tutorial help you solving a problem at your (clients) website and it saved you many hours of work? Great!!
Consider buying Rolf a cup of coffee in return! [ Click here ]


  6 Comments

CMS Made Simple 2.2.2 - Tutorials, tips and tricks - CMSMS 2.2.2

Smooth page scroll to an anchor

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.2
  Last updated: 25-12-2015
  Comments: 6

A2 Webhosting CMS Made Simple