Skip to content

Instantly share code, notes, and snippets.

@m5wdev
Last active December 26, 2018 19:34
Show Gist options
  • Save m5wdev/51ca3dfd546fad5040735e4ebb40a13f to your computer and use it in GitHub Desktop.
Save m5wdev/51ca3dfd546fad5040735e4ebb40a13f to your computer and use it in GitHub Desktop.
Smooth scroll to top [jQuery & CSS]
function scrollToTop() {
var pxFromTop = 800;
$('#footer').append('<div id="goTop">Scroll To Top</div>');
$('#goTop').hide();
$(window).scroll(function () {
if ($(this).scrollTop() > pxFromTop) {
$('#goTop').fadeIn();
} else {
$('#goTop').fadeOut();
}
});
$('#goTop').click(function () {
$('html, body').animate({scrollTop: 0}, 700);
});
}
#goTop {
background-image: url('https://cdn0.iconfinder.com/data/icons/slim-square-icons-basics/100/basics-07-128.png');
background-position: 50% 50%;
background-repeat: no-repeat;
background-size: contain;
cursor: pointer;
text-indent: -9999px;
opacity: .7;
position: fixed;
left: 0;
bottom: 0;
z-index: 10;
* {
transition: all ease-in-out .3s;
}
@include media-breakpoint-up(md) {
display: block;
height: 40%;
width: 6%;
max-width: 80px;
&:hover {
background-image: url('https://cdn0.iconfinder.com/data/icons/slim-square-icons-basics/100/basics-07-128.png'), linear-gradient(to top, white, transparent);
opacity: 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment