Home > Flash, Front End Development, HTML, Javascript > Smooth Scrolling in jQuery

Smooth Scrolling in jQuery

November 30th, 2011


Single page websites seem to be all the rage now a days. Scrolling effects seem to be in high demand. So in this post I’ll show you how to create a smooth scrolling page with jQuery.

What are we building?

We’ll be creating our own fansy smansy website. The nav will sit fixed at the top. Once we click on a nav item it will scroll down to the section. Best of all, if JavaScript is shut off, it will degrade gracefull and just jump down to the section.

The HTML : Content

First thing we need is some content to scroll. This can be anything as long as their is enough of it to scroll….otherwise this tutorial is pointless ;) At the top of each section we’ll need to specify an id (we’ll be using this for our navigation). It can be on anything. Really just put the id on what you want to appear at the top of the page. For this example I put it on the header tags.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<div class="container">
    <div class="section">
        <h1 id="html">HTML</h1>
        <p><!--Your HTML Content Goes HERE --></p>
    </div>
    <div class="section">
        <h1 id="css">CSS</h1>
        <p><!--Your HTML Content Goes HERE --></p>
    </div>
    <div class="section">
        <h1 id="php">PHP</h1>
        <p><!--Your HTML Content Goes HERE --></p>
    </div>
    <div class="section">
        <h1 id="javascript">JavaScript</h1>
        <p><!--Your HTML Content Goes HERE --></p>
    </div>
    <div class="section">
        <h1 id="jquery">jQuery</h1>
        <p><!--Your HTML Content Goes HERE --></p>
    </div>
</div>

The HTML : Navigation

Now that we know what the ids are for each section we can go ahead and use those to create a navigation.

1
2
3
4
5
6
7
<div class="navigation">
    <a href="#html">HTML</a>
    <a href="#css">CSS</a>
    <a href="#php">PHP</a>
    <a href="#javascript">JavaScript</a>
    <a href="#jquery">jQuery</a>
</div>

Now just having this when we click on the navigation it should take us down to our desired location.

The CSS

Nothing to Crazy here. The most important part is the navigation. The position:fixed attribute keeps it at the top. And the margin top on the section class just make sure the content isn’t below the navigation when first loading the page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<style>

/* Made the container thin
so I didn't have to come up
with too much content */

.container{
    width:300px;
    margin:0 auto;
   
}
/* So the sections don't fall
underneath the navigation */

.section{
    margin-top:60px;
}
/*
1. Keeps the position at the top of the page.
2. Adds a background so you can't see text
   underneath the nav.
3. Adds a little padding for breathing room.
*/

.navigation{
    position: fixed;
    background:white;
    padding:20px 20px;
    width:100%;
    margin-top:0px;
    top:0px;
}
</style>

The jQuery (aka The Magic behind smooth scrolling)

The jQuery isn’t overly complicated. We’re just going to use the animate function to a specific location over time. Now we get that location by referencing the item that has the id that the href of our navigation points to. So if our navigation points to “#css”, then we’ll get the position of the item that has the id of “css”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<script>
$(document).ready(function() {

    // Click event for any anchor tag that's href starts with #
    $('a[href^="#"]').click(function(event) {

        // The id of the section we want to go to.
        var id = $(this).attr("href");

        // An offset to push the content down from the top.
        var offset = 60;

        // Our scroll target : the top position of the
        // section that has the id referenced by our href.
        var target = $(id).offset().top - offset;

        // The magic...smooth scrollin' goodness.
        $('html, body').animate({scrollTop:target}, 500);

        //prevent the page from jumping down to our section.
        event.preventDefault();
    });
});
</script>

Putting it all together

Here is everything all in one.

Enjoy!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE HTML>
<html>
<head>

<meta charset="UTF-8">
<title>Smooth Scrolling</title>
<style>

/* Made the container thin
so I didn't have to come up
with too much content */
.container{
    width:300px;
    margin:0 auto;
   
}
/* So the sections don't fall
underneath the navigation */
.section{
    margin-top:60px;
}
/*
1. Keeps the position at the top of the page.
2. Adds a background so you can't see text
   underneath the nav.
3. Adds a little padding for breathing room.
*/
.navigation{
    position: fixed;
    background:white;
    padding:20px 20px;
    width:100%;
    margin-top:0px;
    top:0px;
}
</style>
</head>

<body>
<div class="container">
    <div class="navigation">
        <a href="#html">HTML</a>
        <a href="#css">CSS</a>
        <a href="#php">PHP</a>
        <a href="#javascript">JavaScript</a>
        <a href="#jquery">jQuery</a>
    </div>
    <div class="section">
        <h1 id="html">HTML</h1>
        <p><!--Your HTML Content Goes HERE --></p>
    </div>
    <div class="section">
        <h1 id="css">CSS</h1>
        <p><!--Your HTML Content Goes HERE --></p>
    </div>
    <div class="section">
        <h1 id="php">PHP</h1>
        <p><!--Your HTML Content Goes HERE --></p>
    </div>
    <div class="section">
        <h1 id="javascript">JavaScript</h1>
        <p><!--Your HTML Content Goes HERE --></p>
    </div>
    <div class="section">
        <h1 id="jquery">jQuery</h1>
        <p><!--Your HTML Content Goes HERE --></p>
    </div>
</div>

<script>
$(document).ready(function() {

    // Click event for any anchor tag that's href starts with #
    $('a[href^="#"]').click(function(event) {

        // The id of the section we want to go to.
        var id = $(this).attr("href");

        // An offset to push the content down from the top.
        var offset = 60;

        // Our scroll target : the top position of the
        // section that has the id referenced by our href.
        var target = $(id).offset().top - offset;

        // The magic...smooth scrollin' goodness.
        $('html, body').animate({scrollTop:target}, 500);

        //prevent the page from jumping down to our section.
        event.preventDefault();
    });
});
</script>

</body>
</html>



Top