Im trying to make an animation where I add content that is supposed to be located above the page. When the content is added I want it to appear above the page and then scroll upwards. However when I add my content everything else is instead pushed down. Here is where I get stuck:
const el = (sel, par = document) => par.querySelector(sel);
const els = (sel, par = document) => par.querySelectorAll(sel);
const elBody = el("body");
const elTop = el("#top");
const toggleContact = () => {
elBody.classList.toggle("is-show-contact");
if (elBody.classList.contains("is-show-contact")) {
elTop.scrollIntoView({
behavior: "smooth",
block: "start"
});
}
};
els(".btn-contact").forEach((elBtn) => {
elBtn.addEventListener("click", toggleContact);
});
* {
margin: 0;
box-sizing: border-box;
}
.box {
height: 100dvh;
}
#top {
position: relative;
overflow: auto;
background: #ddd;
transition: height 1s;
height: 0dvh;
.is-show-contact & {
height: 100dvh;
}
}
img{
width:100%;
}
<div id="top">
<h2>CONTACT FORM</h2>
<img src = ".jpg?sz=256">
<button class="btn-contact" type="button">Close</button>
</div>
<div class="box">
<p>Scroll down as well...</p>
<button class="btn-contact" type="button">Contact</button>
</div>
<div class="box">
<button class="btn-contact" type="button">Contact</button>
</div>
<div class="box">
<button class="btn-contact" type="button">Contact</button>
</div>
<div class="box">
<button class="btn-contact" type="button">Contact</button>
</div>
Im trying to make an animation where I add content that is supposed to be located above the page. When the content is added I want it to appear above the page and then scroll upwards. However when I add my content everything else is instead pushed down. Here is where I get stuck:
const el = (sel, par = document) => par.querySelector(sel);
const els = (sel, par = document) => par.querySelectorAll(sel);
const elBody = el("body");
const elTop = el("#top");
const toggleContact = () => {
elBody.classList.toggle("is-show-contact");
if (elBody.classList.contains("is-show-contact")) {
elTop.scrollIntoView({
behavior: "smooth",
block: "start"
});
}
};
els(".btn-contact").forEach((elBtn) => {
elBtn.addEventListener("click", toggleContact);
});
* {
margin: 0;
box-sizing: border-box;
}
.box {
height: 100dvh;
}
#top {
position: relative;
overflow: auto;
background: #ddd;
transition: height 1s;
height: 0dvh;
.is-show-contact & {
height: 100dvh;
}
}
img{
width:100%;
}
<div id="top">
<h2>CONTACT FORM</h2>
<img src = "https://lh3.googleusercontent/-NREkKmTtXX0/AAAAAAAAAAI/AAAAAAAAADg/w2IRnCo5AwQ/photo.jpg?sz=256">
<button class="btn-contact" type="button">Close</button>
</div>
<div class="box">
<p>Scroll down as well...</p>
<button class="btn-contact" type="button">Contact</button>
</div>
<div class="box">
<button class="btn-contact" type="button">Contact</button>
</div>
<div class="box">
<button class="btn-contact" type="button">Contact</button>
</div>
<div class="box">
<button class="btn-contact" type="button">Contact</button>
</div>
I got the idea from a page called yearofmoo, a website that contains codingguides and videos. If you want to check it out the animation happens when you press contact in the menu. Have anyone acheived something simiular? I think I can manage the scroll myself, my question is how I can the content above the viewpoint.
Share Improve this question edited Feb 17 at 15:08 Jack the man asked Feb 17 at 13:45 Jack the manJack the man 318 bronze badges 4
scrollIntoView
– evolutionxbox Commented Feb 17 at 14:02