<header class="header">
<h1>HEADER</h1>
</header>
<main>
<section>SECTION</section>
<section>SECTION</section>
<section>SECTION</section>
<div class="goTop">GoTop</div>
</main>
* {
box-sizing: border-box;
}
.header {
width: 100%;
height: 70px;
background: #000;
color: #fff;
position: fixed;
top: 0;
left: 0;
display:flex;
align-items:center;
justify-content:center;
transition: .2s;
}
.header.hide {
top:-70px;
}
section {
border: 5px solid gray;
height: 500px;
background: #47FF7AE5;
display:flex;
align-items:center;
justify-content:center;
}
.goTop {
position: fixed;
right: 20px;
bottom: 20px;
padding: 40px;
border-radius: 50px;
background: #fff;
opacity: 0;
visibility: hidden;
transition: .2s;
}
.goTop.show{
opacity: 1;
visibility: visible;
}
const header = document.querySelector(".header")
const goTop = document.querySelector(".goTop")
function scrollEvent(event){
const STANDARD = 30;
if(window.scrollY > STANDARD){
header.classList.add("hide")
goTop.classList.add("show")
}else {
header.classList.remove("hide")
goTop.classList.remove("show")
}
}
function moveTop(){
window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth'
});
}
window.addEventListener("scroll", scrollEvent)
goTop.addEventListener("click", moveTop)
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.