Skip to content

Instantly share code, notes, and snippets.

@Martydesign
Created June 19, 2018 15:46
Show Gist options
  • Save Martydesign/511e8d4b34be46041ce9e7354ba2dea0 to your computer and use it in GitHub Desktop.
Save Martydesign/511e8d4b34be46041ce9e7354ba2dea0 to your computer and use it in GitHub Desktop.
Back to top scrolling function - arrow using jQuery.
// make the link to scroll to top of the page
var backToTop = $('<a>', {
href: '#home',
class: 'back-to-top',
html: '<i class="fa fa-caret-up fa-5x"></i>'
});
// add link to the page and establish a scrolling function
backToTop
.hide()
.appendTo('body')
.on('click', function() {
$('html,body').animate({ scrollTop: 0 });
});
// show only if we are enough deep (this sounds little bit pervers) in the page
var win = $(window);
win.scroll(function() {
if ( win.scrollTop() >= 300 ) backToTop.fadeIn();
else backToTop.hide();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment