Skip to content

Instantly share code, notes, and snippets.

@AquisTech
Last active July 11, 2018 14:01
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 AquisTech/36a14409f6926cd78841a25cc4ca9bdc to your computer and use it in GitHub Desktop.
Save AquisTech/36a14409f6926cd78841a25cc4ca9bdc to your computer and use it in GitHub Desktop.
Scroll to top button example
// app/views/layouts/application.html.haml
%div.scroll-to-top
Scroll to Top
// app/assets/javascripts/application.js
$(document).on('turbolinks:load', function() { // OR inside document.ready
// Show/hide scroll to top button
$(document).on('scroll', function(){
if ($(window).scrollTop() > 100) {
$('.scroll-to-top').addClass('show');
} else {
$('.scroll-to-top').removeClass('show');
}
});
// Scroll to top on click
function scrollToTop() {
verticalOffset = typeof(verticalOffset) != 'undefined' ? verticalOffset : 0;
element = $('body');
offset = element.offset();
offsetTop = offset.top;
$('html, body').animate({scrollTop: offsetTop}, 500, 'linear');
}
$('.scroll-to-top').on('click', scrollToTop);
});
// app/assets/stylesheets/application.scss
.scroll-to-top {
border-radius: 22px;
position: fixed;
opacity: 0;
visibility: hidden;
overflow: hidden;
text-align: center;
z-index: 99999;
background-color: #777777;
color: #eeeeee;
right: 30px;
bottom: 30px;
padding: 10px;
font-weight: bold;
cursor: pointer;
transition: all 0.5s ease-in-out;
&.show {
visibility:visible;
cursor:pointer;
opacity: 1.0;
}
}
Scroll to top button example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment