Shaun Mccran

My digital playground

05
F
E
B
2011

JQuery scrolling within a Div

Ever wanted to have your page content scroll based on user interaction? In this article I'll show you how to create a simple JQuery scrollable area that will allow users to click on 'next' style links to view more information. I put this together as most of the JQuery scrolling articles out there deal with scrolling the entire page, but I just want to scroll inside a defined div element.

There is a demo of this here: JQuery scrolling within a Div

The idea here is to split your page content into several smaller 'bite size' chunks. Then you can add a 'more' style link at the bottom of each piece of content that will scroll down.

I'm using the smooth-scroll JQuery plugin, and the easing plugin here. The JQuery below will look for a click event on any hyperlink elements with a class of 'scrollsomething'. It will get the href value and use it to scroll to the first element with a matching id, inside the div 'scrollme'.

view plain print about
1<s/cript src="easing.js" type="text/javascript"></script>
2<s/cript src="jquery.smooth-scroll.js" type="text/javascript"></script>
3<s/cript type="text/javascript">
4    $(document).ready(function() {
5
6 $('a.scrollsomething').click(function() {
7
8 var jumpto = $(this).attr("href");
9
10 $.smoothScroll({
11 scrollElement: $('div.scrollme'),
12 scrollTarget: jumpto
13
14 });
15 return false;
16 });
17 });
18
19</script>

Now construct your page content, and make sure that the your links to each of the next content sections have a class of 'scrollsomething' and a href value that matches the id you want it to scroll to.

view plain print about
1<div class="scrollme">
2
3    <h3 id="start">First paragraph</h3>
4    Lorem ipsum dolor...<br>
5    <a href="#findme" class="scrollsomething">Next Content</a>
6    
7    <div class="phat-space"><!-- --></div>
8    
9    <h3 id="findme">Second paragraph</h3>
10    Lorem ipsum dolor...
11    <a href="#findme2" class="scrollsomething">Next Content</a>
12
13    <div class="phat-space"><!-- --></div>
14
15    <h3 id="findme2">Paragraph 3</h3>
16    Lorem ipsum dolor...
17    <a href="#start" class="scrollsomething">Top</a>
18
19    <div class="phat-space"><!-- --></div>
20</div>
21
22<style type="text/css">
23 .container {
24 margin: 40px auto;
25 width: 400px;
26 }
27 .scrollme {
28 height: 200px;
29 overflow: auto;
30 padding-left: 8px;
31 border: 1px solid #999;
32 }
33
34    .phat-space {display: block; height: 150px;}
35
36</style>

The good thing about this is that it will also degrade nicely, if a user has JavaScript turned off the browser will still use the old method of jumping to the named target.

Extending with Google Analytics

As you are allowing users to selectively view different content sections you could also add in a Google Analytics call within the JQuery function to track each content view as a page impression, a bit like this:

view plain print about
1$.smoothScroll({
2scrollElement: $('div.scrollme'),
3scrollTarget: jumpto
4
5});
6
7pageTracker._trackPageview(jumpto);
8
9return false;

There is a demo of this here: JQuery scrolling within a Div

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Rand()'s Gravatar That looks slick! Great job!
# Posted By Rand() | 06/02/2011 03:38
dedicated server reseller's Gravatar links to each of the next content sections have a class of 'scrollsomething' and a href value that matches the id you want it to scroll to.
# Posted By dedicated server reseller | 19/12/2015 23:31
cheap assignment writing help's Gravatar Firstly create a CurrencyFormatter object, and specify the currency symbol, and any other formatting parameters that you require.
# Posted By cheap assignment writing help | 26/12/2015 02:29
this link's Gravatar Good to become visiting your weblog again, it has been months for me. Nicely this article that i've been waited for so long.
# Posted By this link | 28/12/2015 02:09
supreme garcinia cambogia reviews's Gravatar links to each of the next content sections have a class of 'scrollsomething' and a href value that matches the id you want it to scroll to.
# Posted By supreme garcinia cambogia reviews | 28/12/2015 05:24
this link's Gravatar Your site and show the kids check out up here generally. I m fairly positive there likely to be informed a great deal of new stuff here than anyone.
# Posted By this link | 29/12/2015 01:57
taxi from delhi to jaipur's Gravatar I m fairly positive there likely to be informed a great deal of new stuff here than anyone.
# Posted By taxi from delhi to jaipur | 05/01/2016 02:32
selling products on amazon's Gravatar I have feel that this blog is really have all those quality that qualify a blog to be a one.I wanted to leave a little comment to support you and wish you a good continuation.
# Posted By selling products on amazon | 09/01/2016 02:52
Back to top