Last active
June 8, 2020 08:36
-
-
Save b-aleksei/ac657ea9e6ee1fa606a00abc1b971e75 to your computer and use it in GitHub Desktop.
scroll-to-top
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function trackScroll() { | |
let scrolled = window.pageYOffset; | |
let coords = document.documentElement.clientHeight; | |
if (scrolled > coords) { | |
goToTop.classList.add("to_top-show"); | |
} | |
if (scrolled < coords) { | |
goToTop.classList.remove("to_top-show"); | |
} | |
} | |
function backToTop() { | |
if (window.pageYOffset > 0) { | |
window.scrollBy(0, -80); | |
setTimeout(backToTop); | |
} | |
} | |
let goToTop = document.querySelector(".to_top"); | |
window.addEventListener("scroll", trackScroll); | |
goToTop.addEventListener("click", backToTop); | |
/* | |
<button class="to_top" type="button" aria-label="прокрутка вверх"></button> | |
.to_top { | |
display: none; | |
position: fixed; | |
bottom: 30px; | |
right: 10px; | |
width: 40px; | |
height: 40px; | |
text-align: center; | |
line-height: 40px; | |
box-shadow: 0 0 10px 1px rgba(103, 58, 39, 0.8); | |
border: none; | |
background-color: transparent; | |
cursor: pointer; | |
border-radius: 5px; | |
z-index: 999; | |
&::after { | |
content: ''; | |
position: absolute; | |
top: 45%; | |
left: 50%; | |
transform: translate(-50%, -50%); | |
border-top: none; | |
border-bottom: 15px solid $basic; | |
border-left: 7px solid transparent; | |
border-right: 7px solid transparent; | |
} | |
} | |
.to_top:active { | |
box-shadow: 0 0 5px 3px rgba(103, 58, 39, 0.8); | |
&::after { | |
border-bottom-color: $accent; | |
} | |
} | |
.to_top:focus::after { | |
outline: none; | |
} | |
.to_top-show { | |
display: block; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment