I am attempting to reveal divs as the user scrolls through the page. I am attempting to do this using javascript and css without any additional libraries.
Currently, I have a setup using jQuery that gets the job done but I am having issues finding a javascript only solution. I do not want the div to appear in a certain position, just simply as it appears in the user's viewport.
I am using opacity: 0 to hide the div, and then using $(this).animate({'opacity':'1'},500); to display.
I have been unable to find a similar javascript option that does not use a library or jQuery.
Can I achieve the code below using javascript?
A working jquery solution example is :
$(document).ready(function() {
/* Every time the window is scrolled ... */
$(window).scroll( function(){
/* Check the location of each desired element */
$('.scroll-in').each( function(i){
var bottom_of_object = $(this).offset().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* If the object is completely visible in the window, fade it it */
if( bottom_of_window > bottom_of_object ){
$(this).animate({'opacity':'1'},500);
}
});
});
});
#container
{
height:2000px;
}
#container div
{
margin:50px;
padding:50px;
background-color:blue;
}
.scroll-in
{
opacity:0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
<div>Show</div>
<div>Show</div>
<div class="scroll-in">Fade In</div>
<div class="scroll-in">Fade In</div>
<div class="scroll-in">Fade In</div>
</div>
transition: opacity 1.0s ease-in;