Skip to content

Instantly share code, notes, and snippets.

@omniosi
Last active December 12, 2015 09:19
Show Gist options
  • Save omniosi/4751038 to your computer and use it in GitHub Desktop.
Save omniosi/4751038 to your computer and use it in GitHub Desktop.
animated scrolling to links in jQuery
The function for the horizontal scrolling effect is the following:
$(function() {
$('ul.nav a').bind('click',function(event){
var $anchor = $(this);
/*
if you want to use one of the easing effects:
$('html, body').stop().animate({
scrollLeft: $($anchor.attr('href')).offset().left
}, 1500,'easeInOutExpo');
*/
$('html, body').stop().animate({
scrollLeft: $($anchor.attr('href')).offset().left
}, 1000);
event.preventDefault();
});
});
And the function for the vertical scrolling effect is the following:
$(function() {
$('ul.nav a').bind('click',function(event){
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top
}, 1500,'easeInOutExpo');
/*
if you don't want to use the easing effects:
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top
}, 1000);
*/
event.preventDefault();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment