Last active
August 29, 2015 14:04
-
-
Save turboMaCk/f34c3391af1ceb7b3ac8 to your computer and use it in GitHub Desktop.
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
(function() { | |
var defaultDuration = 500; | |
var scrollToElement = function($element, duration) { | |
// use body if no $element is defined | |
// use fdefault duration if is not sad different | |
$element = $element || $('body'); | |
duration = duration || defaultDuration; | |
// stop if the is no such element | |
if ($element.length === 0) return false; | |
// set offset (with navbar height) | |
var offset = $element.offset().top - $('#nav-bar').outerHeight(); | |
// scroll | |
$('html, body').animate({ | |
scrollTop: offset | |
}, duration); | |
}; | |
// jquery function | |
$.fn.scrollToElement = function(duration) { | |
scrollToElement($(this), duration); | |
}; | |
// default use on this class | |
$('a.scroll-to').on('click', function(e) { | |
e.preventDefault(); | |
var selector = $(this).attr('href'); | |
$(selector).scrollToElement(); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment