Skip to content

Instantly share code, notes, and snippets.

@arieljatib
Last active October 10, 2015 13:24
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 arieljatib/6af8868b4147121d876c to your computer and use it in GitHub Desktop.
Save arieljatib/6af8868b4147121d876c to your computer and use it in GitHub Desktop.
Set Distance From Bottom, Stop Fixed Position
// Adjust location of 'back to top' near the bottom of the page
function notTooNearTheBottom() {
var viewportHeight = $(window).height();
var actionsArea = $(window).scrollTop() + viewportHeight;
var bottomBoundary = $(document).height() - 90; // this is our 'safe area'
if( actionsArea > bottomBoundary ) {
// console.log('near the bottom - move 'up to top', hide 'comments');
$('#action-top').css('top', bottomBoundary);
}
else {
$('#action-top').css('top',''); //clear the inline css
}
}
// watch the scroll event
$(window).scroll(function(){
//but - don't watch it too much. let's clear the timeout
clearTimeout(window.scroll_timeout); //.scroll_timeout is the variable
window.scroll_timeout = setTimeout(function() {
// function to call
notTooNearTheBottom();
}, 20); // wait 'this long' to fire
});
#go-to-the-top {
top: 88%; // alternative to js - keeping it near the bottom
transition: top 250ms; // animate the transition
a:hover:after, a:active:after { // add nice label on hover
content: '\A top';
font-size: 1.5rem;
white-space: pre;
vertical-align: top;
}
}

from the Bottom

Started from the bottom, now we're here.

This little bit of js and css makes it so that the 'back to the top' link will only go so far down on the page. It was born out of not wating the link to lay over the footer section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment