In this tutorial, you will learn how to create three vertical bars animation/transition overlay with CSS and JavaScript. First part is an animation purely by CSS. The second part and third part are transitions controlled by scroll with JavaScript. Watch the video below for a detailed tutorial.
<div class="container">
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
</div>
<script>
var nums = [2.9, 4.1, 2.1, 4.4, 2.8]
function changeHeight() {
const bars = document.querySelectorAll('.bar2');
var scroll = (window.pageYOffset / 5);
bars.forEach((col, key) => {
var height = Math.min(100 - (scroll * nums[key]));
col.style.height = height + '%';
})
}
window.addEventListener('scroll', function() {
requestAnimationFrame(changeHeight);
})
</script>
<script>
function changeHeight() {
const bars = document.querySelectorAll('.bar3');
var height = Math.max(100 - window.pageYOffset);
bars.forEach((col, key) => {
if(height > 0) {
col.style.height = height + '%';
} else {
col.style.height = 0;
}
})
}
window.addEventListener('scroll', function() {
requestAnimationFrame(changeHeight);
})
</script>
body {
margin: 0;
}
.container {
background-image: url('image.jpg');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
height: 140vh;
display: grid;
grid-template-columns: repeat(5, 1fr);
}
.bar {
background: #FBD84A;
height: 100vh;
transition: height 2.3s;
animation: animate forwards;
}
.bar:nth-child(1) {
animation-duration: 3s;
}
.bar:nth-child(2) {
animation-duration: 4s;
}
.bar:nth-child(3) {
animation-duration: 3.5s;
}
.bar:nth-child(4) {
animation-duration: 2.9s;
}
.bar:nth-child(5) {
animation-duration: 3.1s;
}
@keyframes animate {
from {
height: 100vh;
}
to {
height: 0;
}
}
/* Index 2 */
.bar2 {
background: #FBD84A;
height: 100vh;
transition: height 2.3s;
}
/* Index 3 */
.bar3 {
background: #FBD84A;
height: 100vh;
transition: height 2s;
position: fixed;
width: 20%;
height: 100vh;
}
.bar3:nth-child(1) {
top: 0;
}
.bar3:nth-child(2) {
top: 0;
left: 20%;
transition-delay: 0.2s;
}
.bar3:nth-child(3) {
bottom: 0;
left: 40%;
}
.bar3:nth-child(4) {
bottom: 0;
left: 60%;
transition-delay: 0.2s;
}
.bar3:nth-child(5) {
top: 0;
left: 80%;
}