This Vanilla JavaScript code snippet helps you to create slide in animation on scroll event. It gets the page offset and applies animation on scroll event when it comes into view. The div element can be styled according to your needs.
How to Create Slide in on Scroll in Vanilla JavaScript
1. First of all, create the HTML structure as follows:
<div class="left" id="right"> Your content goes here... </div> <div class="right" id="left">Your content goes here... </div> <hr> <footer> <div class="foot" id="foot"> Your content goes here... </div> </footer>
2. After that, add the following CSS styles to your project:
* { margin: 0; padding: 0; } body { z-index: 0; body: 100%; } #naruto { background-image: url("https://wallpaperstudio10.com/static/wpdb/wallpapers/1920x1080/174283.jpg"); background-color: #cccccc; background-attachment: fixed; background-repeat: no-repeat; background-position: center; background-size: cover; width: 100%; height: 700px; opacity: 0.7; position: relative; Â Â Â Â z-index: 1; } .left, .right { border: 2px darkgrey solid; background: lightgrey; border-radius: 10px; margin-bottom: 50px; margin-top: 50px; margin-left: 20%; display: flex; width: 50%; position: relative; } #left { opacity: 0; padding: 30px; } #right { opacity: 0; padding: 30px; } .foot { border: 2px darkgrey solid; background: black; border-radius: 10px; color: orange; font-size: 20px; line-height: 50px; padding: 50px; width: 50%; margin-top: 30px; display: flex; margin-left: 45%; margin-bottom: 50px; left: -200px; opacity: 0; } .animation-right { animation: righty 2s forwards; position: relative; } @keyframes righty { 0% { opacity: 0; right: -200px; } 100% { opacity: 1; right: 60px; } } .animation-left { animation: lefty 2s forwards; position: relative; } @keyframes lefty { 0% { opacity: 0; left: -200px; } 100% { opacity: 1; left: 60px; } } .animation-top { animation: toping 2s forwards; position: relative; animation-delay: 1s; } @keyframes toping { 0% { opacity: 0; bottom: -200px; } 100% { opacity: 1; bottom: 2px; } }
3. Finally, add the following JavaScript code and done.
var left_div = document.getElementById('left'); var right_div = document.getElementById('right'); var bottom_div = document.getElementById('foot'); window.addEventListener('scroll', animation); function animation (e){ var left_div_pos = left_div.offsetTop; var right_div_pos = right_div.offsetTop; var bottom_div_pos = bottom_div.offsetTop; var window_pos = window.pageYOffset; if (window_pos > left_div_pos-500){ left_div.classList.add('animation-left'); } if(window_pos> right_div_pos-500){ right_div.classList.add('animation-right'); } if(window_pos >bottom_div_pos -500) { bottom_div.classList.add('animation-top'); } }
That’s all! hopefully, you have successfully integrated this slide in on scroll code snippet into your project. If you have any questions or are facing any issues, please feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.