Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make the content appear on scrolling down the webpage?

I have seen website where content appear as I scroll down the webpage. I have this code but its not working. Can you guide and give proper explanation.

$(document).ready(function(){
  //Take your div into one js variable
  var div = $("#divToShowHide");
  //Take the current position (vertical position from top) of your div in the variable
  var pos = div.position();
  //Now when scroll event trigger do following
  $(window).scroll(function () {
   var windowpos = $(window).scrollTop();
   //Now if you scroll more than 100 pixels vertically change the class to AfterScroll
   // I am taking 100px scroll, you can take whatever you need
   if (windowpos >= (pos.top-100)) {
     div.addClass("AfterScroll");
   }
   //If scroll is less than 100px, remove the class AfterScroll so that your content will be hidden again 
   else {
     div.removeClass("AfterScroll");
   }
   //Note: If you want the content should be shown always once you scroll and do not want to hide it again when go to top agian, no need to write the else part
 });
});
.BeforeScroll
{
  height: 100px; /*Whatever you want*/
  width: 100%; /*Whatever you want*/
  display: none;
}


/*Use this class when you want your content to be shown after some scroll*/
.AfterScroll
{
  height: 100px; /*Whatever you want*/
  width: 100%; /*Whatever you want*/
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "divToShowHide" class = "BeforeScroll">Content you want to show hide on scroll
 </div>
like image 941
Varun Jain Avatar asked Dec 06 '22 17:12

Varun Jain


2 Answers

If you would like to make some animation also, I'll suggest you AOS

It's an Animate On Scroll Library and you can make the content appear on scrolling down


How to use:

adding "data-aos="animation name"" to HTML tags would do the trick:

<div class="topBar" data-aos="fade-in">

after you add in :

<link href="https://cdn.rawgit.com/michalsnik/aos/2.1.1/dist/aos.css" rel="stylesheet">

in head section and add:

<script src="https://cdn.rawgit.com/michalsnik/aos/2.1.1/dist/aos.js"></script>

before the end of body tag.

a quick example: https://codepen.io/karenmio/pen/KxGewG

there are variations that you can learn from this but the related site does try to sell you courses, let me know if this link is not proper/or take it out: https://codepen.io/SitePoint/pen/MmxQMG

like image 87
id3vz Avatar answered Dec 08 '22 06:12

id3vz


I will give a an a example with scrollrevealjs

include the library like that:

<script src="https://cdn.jsdelivr.net/scrollreveal.js/3.3.1/scrollreveal.min.js"></script>

then in your js file just init the library

window.sr = ScrollReveal();

and then just add the class of the component you like to animate

sr.reveal('.YourClass1');
sr.reveal('.YourClass2');

here you will find how to work with this library :)

https://github.com/jlmakes/scrollreveal.js

like image 43
Asen Sokolov Avatar answered Dec 08 '22 07:12

Asen Sokolov