Email This List Email This List Print This List Print This List

How to cre­ate jQuery scroll­To & loc­alscroll

*UPDATE: new browser ver­sions will do this auto­mat­ic­ally, as long as you put a <div> some­where and make a link to it!

Start with an empty html file. Give it any name you want, as long as it ends with .html or .htm.

Double click the code win­dow to make the code easi­er to copy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>My jQuery scrollTo & localscroll Demo</title>
</head>
<body>
</body>
</html>

Inside the head tags we will have to load jQuery, scroll­To and loc­alscroll. It’s import­ant to do it in the order I just wrote. Since scroll­To and loc­alscroll are plu­gins of jQuery, they first need jQuery to be loaded for them to work.

Copy this code into your head tag of your html doc­u­ment.

1
2
3
4
5
6
7
<!-- Load jQuery -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <!-- Load ScrollTo --><script type="text/javascript" src="http://flesler-plugins.googlecode.com/files/jquery.scrollTo-1.4.2-min.js"></script>
    <!-- Load LocalScroll -->

We’re now load­ing jQuery and the plu­gins dir­ectly from google. You can also save them to your com­puter and load them loc­ally. If you want your html file to work off­line, this is a must. Now we’re going to set up links and divs to scroll to. Pase the fol­low­ing into the body tag of your html doc­u­ment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre>
<h3>Links to colored boxes</h3>
<pre>
</pre>
<div id="box-links"><a href="#box1">Link to box #1</a> <a href="#box2">Link to box #2</a> <a href="#box3">Link to box #3</a> <a href="#box4">Link to box #4</a></div>
<pre>
</pre>
<div id="box1" class="box">Box #1</div>
<pre>
</pre>
<div id="box2" class="box">Box #2</div>
<pre>
</pre>
<div id="box3" class="box">Box #3</div>
<pre>
</pre>
<div id="box4" class="box">Box #4</div>
<pre>

Let’s set up the actu­al script that will enable the smooth scrolling. Paste this code inside your head tag at the very bot­tom (above “</​head>”).

1
2
3
4
5
6
7
8
9
10
11
12
    <!-- javascript that will initiate jQuery and the LocalScroll plugin --><script type="text/javascript">// <![CDATA[
    // When the document is loaded...
    $(document).ready(function()
    {
        // Scroll the whole document
        $('#box-links').localScroll({
           target:'body'
        });
    });
// ]]></script>

Your doc­u­ment prob­ably looks a bit dull now. Only text. We need some CSS! Put this code in your head tag as well:

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
<style type="text/css">
    /* CSS for the big boxes */
    .box {
        width: 300px;
        height: 300px;
        color: #fff;
        padding: 10px;
        margin: 100px 0 0 0;
    }
    #box1 {
        margin: 300px 0 0 0;
        background: blue;
    }
    #box2 {
        background: red;
    }
    #box3 {
        background: green;
    }
    #box4 {
        background: gray;
    }
    /* CSS for the small boxes that will scroll inside a div */
    #small-box-container {
        border: 1px solid black;
        padding: 20px;
        width: 300px;
        height: 200px;
        overflow: scroll;
    }
    .small-box {
        color: #fff;
        padding: 10px;
        width: 200px;
        height: 200px;
        margin: 0 0 50px 0;
    }
    #small-box1 {
        background: blue;
    }
    #small-box2 {
        background: red;
    }
    #small-box3 {
        background: green;
    }
    </style>

Now, when you click on a link. Your whole doc­u­ment should smoothly scroll down to a colored box.

Here’s the whole code for the html doc­u­ment. I’ve also added a div where you scroll to boxes inside.

Related Post

admin has written 133 articles