Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  139] [ 5]  / answers: 1 / hits: 28650  / 8 Years ago, sat, july 30, 2016, 12:00:00

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>




More From » jquery

 Answers
11

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


[#61202] Thursday, July 28, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leiaf

Total Points: 10
Total Questions: 101
Total Answers: 84

Location: Guam
Member since Tue, Nov 29, 2022
2 Years ago
;