<div class="btn_to_top">
<svg class="i_to_top">
<use xlink:href="#btnToTop"></use>
</svg>
</div>
<svg display="none">
<symbol id="btnToTop" viewBox="0 0 7 12">
<path d="M7 1.41L5.66802 0L0 6L5.66802 12L7 10.59L2.67341 6L7 1.41Z"/>
</symbol>
</svg>
// to_top_btn
.btn_to_top {
position: fixed;
bottom: 20px;
right: 20px;
display: flex;
align-items: center;
justify-content: center;
width: 50px;
height: 50px;
background: teal;
border: 1px solid grey;
cursor: pointer;
opacity: 1;
visibility: visible;
transition: all .25s ease-out;
.i_to_top {
width: 7px;
height: 12px;
transform: rotateZ(90deg);
fill: black;
transition: all .25s ease-out;
}
&:hover {
background: blue;
.i_to_top {
fill: teal;
}
}
}
.btn_to_top._active {
opacity: 1;
visibility: visible;
}
// scroll_to_top
const toTopBtn = document.querySelector('.btn_to_top');
if (toTopBtn) {
toTopBtn.onclick = () => {
window.scrollTo(0, 0);
};
// OR
window.onscroll = () => {
if(window.scrollY > 1000) {
toTopBtn.classList.add('_active')
} else {
toTopBtn.classList.remove('_active')
}
};
// OR (smooth scroll)
toTopBtn.onclick = () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
})
}
}
// В css для body добавить scroll-behavior: smooth;