Smooth Scrolling with jQuery
Posted: December 24th, 2012 | Author: Julia | Filed under: Tips and Tricks | Tags: jQuery, scrolling lengthy content, smooth scrolling | 4 Comments »If you ever have to read a long document, scrolling all the way through it to find what you need can be annoying. This case a smooth scroller seems to be a solution, it helps with navigation and you enhance usability on the website with sleek navigation controls.
As for my mind, such sweet )) scrolling is fine for pages which list all links at the top and the relevant content – below (FAQ pages, Terms pages) or ‘back to top’ links.
This technique is based on the jQuery library.
There are 2 parts of scrolling script: the first script is for scrolling to the very top of the page and the second one – to scroll to the certain place of the page.
1. Scroll the page to the top
At the body section I defined elements with class ‘anchorLink’:
-
<a href="#" class="anchorLink">Top ↑</a>
When the link with the class ‘anchorLink’ is clicked, the smooth scroll effect is created using the scrollTop function (I set the speed as ‘slow’):
-
$(".anchorLink").click(function () {
-
$("html, body").animate({ scrollTop: 0 }, "slow");
-
return false; // prevents the default action of the event being triggered
-
});
2. Scroll the page to the certain element
I set elements with class ‘anchorLink2’ at the body section:
-
<a class="anchorLink2" data-atr="tab1" href="#tab1">Tab1</a>
The function is for every element with class ‘anchorLink2’ – when it’s clicked:
-
jQuery('.anchorLink2').click(function (event) {
-
-
});
To tie class ‘anchorLink2’ to the relevant paragraph (for example, for the first one), I add custom attribute ‘data-atr’ and href attribute:
-
data-atr="tab1" href="#tab1"
and at the paragraph section –
-
<a id="tab1"> </a>
The jQuery script is
-
jQuery('.anchorLink2').click(function (event) {
-
-
event.stopPropagation();
-
var idTo = jQuery(this).attr('data-atr'); // My custom attribute 'data-atr'
-
var Position = jQuery('[id="' + idTo + '"]').offset().top;
-
// Scroll down to 'Position'
-
jQuery('html, body').animate({ scrollTop: Position }, 'slow');
-
// Stop the link from acting like a normal anchor link (i.e. it prevents the user following the link)
-
return false;
-
});
The full solution is
-
$(document).ready(function () {
-
-
$(".anchorLink").click(function () {
-
$("html, body").animate({ scrollTop: 0 }, "slow");
-
return false;
-
});
-
-
-
jQuery('.anchorLink2').click(function (event) {
-
event.stopPropagation();
-
var idTo = jQuery(this).attr('data-atr');
-
var Position = jQuery('[id="' + idTo + '"]').offset().top;
-
jQuery('html, body').animate({ scrollTop: Position }, 'slow');
-
return false;
-
});
-
});
Live example: check the example >>
Great post. Works perfect.
But I have a question.
The action is triggered from a click on the link “tab1”.
What I also need, is to trigger the action from a GET-parameter. For Example:
http://www.sweet-web-design.com//examples/scrolling.html&tab=7
When another page is linking to this page is should scroll to tab 7.
So how to I activate the trigger within PHP depending on a variabele?
Is that possible?
Thanks in advance.
Hi Eduard,
I think, you could use header(“Location: http://” . $_SERVER[‘HTTP_HOST’] . “/your-page#tab7/”); for redirect to the required page (and set there). To achieve the smooth scrolling effect you should give variables via GET parameters and process custom jQuery at the target page. So yes, that’s possible and requires custom development.
Hi Eduard,
I think, you could use header(“Location: http://” . $_SERVER[‘HTTP_HOST’] . “/your-page#tab7/”); for redirect to the required page (and set there). To achieve the smooth scrolling effect you should give variables via GET parameters and process custom jQuery at the target page. So yes, that’s possible and requires custom development.
I have been looking for a code that does this all day. None of the code on other sites worked but this did. So thank you so much.