Skip to content

Instantly share code, notes, and snippets.

@ArtemSites
Last active March 25, 2022 15:51
Show Gist options
  • Save ArtemSites/d742beed1bd244c681b8644e90b5aa62 to your computer and use it in GitHub Desktop.
Save ArtemSites/d742beed1bd244c681b8644e90b5aa62 to your computer and use it in GitHub Desktop.
Кнопка плавного скроллинга до верхушки html документа | Smooth scrolling button to the top of the html document
<div id="up-arrow"></div>
</footer>
/**
* @source: https://stackoverflow.com/questions/4817029/whats-the-best-way-to-detect-a-touch-screen-device-using-javascript#answer-4819886
**/
function isTouchDevice() {
return ( ('ontouchstart' in window) || (navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0) );
}
// Go Top Button
$(function() {
const $goTop = $('#goTop');
const speedMs = 500;
const showTriggerPx = 350;
if (isTouchDevice()) {
$goTop.addClass('--touch');
}
goTopInitialize();//on load
window.onscroll = function () {
goTopInitialize();
}
function goTopInitialize() {
let scrolled = window.pageYOffset || document.documentElement.scrollTop;
$goTop
.show()
.css('opacity', '.7');
if (scrolled <= showTriggerPx || scrolled === 0) {
$goTop
.hide()
.css('opacity', '0.3');
}
}
$goTop.on('click', function() {
$('html, body').animate(
{
scrollTop: 0
},
speedMs
);
$(this).hide();
return true;
});
});
#up-arrow {
background: #fff url(../img/up-arrow.svg) no-repeat center;
background-size: 60%;
padding: 30px;
position: fixed;
right: 20px;
bottom: 15px;
transition: 400ms;
}
#up-arrow:hover {
transform: scale(1.1);
opacity: 0.9 !important;
cursor: pointer;
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment