Last active
May 31, 2017 08:11
-
-
Save souljorje/c63c62d763d40204bcb104b9eb2a62b5 to your computer and use it in GitHub Desktop.
Smooth scroll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Smooth scroll | |
// 1) add .smooth-scroll to button | |
// 2) set data-section="#id(of anchor)" | |
// 3) | |
(function($){ | |
$.fn.scrollingTo = function( opts ) { | |
var defaults = { | |
animationTime : 1000, | |
easing : '', | |
callbackBeforeTransition : function(){}, | |
callbackAfterTransition : function(){}, | |
}; | |
var config = $.extend( {}, defaults, opts ); | |
$(this).click(function(e){ | |
var eventVal = e; | |
e.preventDefault(); | |
var $section = $(document).find( $(this).data('section') ); | |
if ( $section.length < 1 ) { | |
return false; | |
}; | |
if ( $('html, body').is(':animated') ) { | |
$('html, body').stop( true, true ); | |
}; | |
var scrollPos = $section.offset().top; | |
if ( $(window).scrollTop() == scrollPos ) { | |
return false; | |
}; | |
config.callbackBeforeTransition(eventVal, $section); | |
$('html, body').animate({ | |
'scrollTop' : (scrollPos+'px' ) | |
}, config.animationTime, config.easing, function(){ | |
config.callbackAfterTransition(eventVal, $section); | |
}); | |
}); | |
}; | |
}(jQuery)); | |
jQuery(document).ready(function(){ | |
(function(){ | |
jQuery('.smooth-scroll').scrollingTo(); | |
}()); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment