Created
July 2, 2015 13:48
-
-
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.
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() { | |
$('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