jQuery Animated Scroll to DIV

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
So, before I start I'd just like to point that this tutorial's credit does not all go to me, I've used the script from css-tricks. I am simply just reposting, in order to try and explain it in an easier way for others.

Basically, this is a simple effect that can often make websites feel that little bit "slick". How it works: When a user is to click a link, they will animate a scroll to a specific area - which you have set up.

Here's how we do it. In order to start, I suggest you have your page set up already - with the sections and navigation. Lets start..

Firstly, create a new file and call it anything you'd like. In the file, add this script:
Code:
$(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1500);
        return false;
      }
    }
  });
});
This is the jQuery code for your site. This is essentially what will cause the animation effect when a user clicks the link. All you have to do now, is set up your page so that is uses this correctly.

First, we're going to link your jQuery and the jQuery library - if you haven't already. This can either be added before the </head> section of your site or just before you close your </body> tag. An example of this can be seen here:
Code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="location/javascript.js"></script>

First line: Links to the jQuery library which is needed in order for any of the jQuery elements to work. Im using an external file hosted on google, alternatively you can download jQuery .

Second line: This links to where your newly created jQuery file is located. So remember to change "location/javascript.js" to your file location!


Now we've linked it, all we have to do it add a couple of things and we're complete. Head on over in to your navigation part of your site and locate the link you want to use.
It should look something like this:
HTML:
<a href="" title="this will scroll">Your Scrolling Link</a>
Notice how the href attribute is empty? This is what you are going to fill with what we need. As this is an example, we'll fill with this:
HTML:
<a href="#scroll" title="this will scroll">Your Scrolling Link</a>
Notice now how the href attribute has "#scroll" assigned to it? This will be your link.
The # stands for ID, therefor meaning it will scroll to an element with the ID of scroll.

Now all that is left to do is to set up that element with the ID of scroll. This can be a <div> tag, a <section> tag or even a <h1> header tag. An example is shown below:
Code:
<section id="scroll"></section>
I've decided to use the <section> tag. As I have assigned the ID of scroll to this element, it'll mean that when the navigation link we assigned earlier is clicked it will jump to the this ID element. However, it will scroll because of the jQuery we added earlier on in the tutorial.

You are able to do this with any name for an ID. For example, you could switch that round so the link was "#scroll2" but just make sure what ever you change your navigation to, you change your element you want to scroll to, as well.

I now realise this is a big ass piece of writing and I'm a sad fucker for doing this. Anyway, hope it helps.. If you follow this step by step, achieving the end product will be easier. If you get stuck, feel free to comment below. Thanks.
 
Last edited:

Snappy

^^^^UpHosting^^^^
Aug 29, 2013
521
43
Thanks a lot for this tutorial will be adding this right now to my site

Live preview here: click about
 
Last edited by a moderator:

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
I assume you meant 'Scroll' rather than 'School' in your title, so I've changed it for you.

Can you show us a demo of this?
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
I assume you meant 'Scroll' rather than 'School' in your title, so I've changed it for you.

Can you show us a demo of this?
Ah thanks for that mate.
A demo can be seen here: - just click the "send me updates" to see an example.

Sorry for bad demo, on phone and its the only example I can remember
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
Another demo
Thanks for that.

Just a heads up, this WILL work on all modern browsers but if for some reason the user has JavaScript disabled, your navigation will still work, only without the animated effect.
 

Snappy

^^^^UpHosting^^^^
Aug 29, 2013
521
43
Thanks for that.

Just a heads up, this WILL work on all modern browsers but if for some reason the user has JavaScript disabled, your navigation will still work, only without the animated effect.
Also works on IOS which is a bonus!
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Thanks for that.

Just a heads up, this WILL work on all modern browsers but if for some reason the user has JavaScript disabled, your navigation will still work, only without the animated effect.
It works because it's basic HTML because the hash-href of the anchor is the same as the DIV ID, I used to do this on piczo years ago, of course without the jQuery side of things.
 

Users who are viewing this thread

Top