Vanilla JavaScript Slide in on Scroll

Vanilla JavaScript Slide in on Scroll
Code Snippet: Scroll Animation with pure JavaScript
Author: Suraj Sharma
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 5,635
License: MIT
Edit Code online: View on CodePen
Read More

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.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X