Skip to content

Instantly share code, notes, and snippets.

@louisnovick
Created July 2, 2015 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louisnovick/6c5f6aaca71374e9d13d to your computer and use it in GitHub Desktop.
Save louisnovick/6c5f6aaca71374e9d13d to your computer and use it in GitHub Desktop.
Smooth Scroll. Create a back to top button that appears based on window height.
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
var _throttleTimer = null;
var _throttleDelay = 100;
var $window = $(window);
var $document = $(document);
$document.ready(function () {
$window
.off('scroll', ScrollHandler)
.on('scroll', ScrollHandler);
});
function ScrollHandler(e) {
//throttle event:
clearTimeout(_throttleTimer);
_throttleTimer = setTimeout(function () {
//console.log('scroll');
//do work
if ($window.scrollTop() + $window.height() > $document.height() - 1000) {
$(".back-up").addClass("show");
}
else {
$(".back-up").removeClass("show");
}
}, _throttleDelay);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment