Skip to content

Instantly share code, notes, and snippets.

@turboMaCk
Last active August 29, 2015 14:04
Show Gist options
  • Save turboMaCk/f34c3391af1ceb7b3ac8 to your computer and use it in GitHub Desktop.
Save turboMaCk/f34c3391af1ceb7b3ac8 to your computer and use it in GitHub Desktop.
(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