简体   繁体   English

如何使内容在向下滚动网页时出现?

[英]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> 

If you would like to make some animation also, I'll suggest you AOS 如果您还想制作一些动画,我建议您AOS

It's an Animate On Scroll Library and you can make the content appear on scrolling down 这是一个Animate On Scroll Library,您可以使内容在向下滚动时显示


How to use: 如何使用:

adding "data-aos="animation name"" to HTML tags would do the trick: 将“ data-aos =”动画名称“”添加到HTML标记中可以达到目的:

<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 一个简单的例子: 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 您可以从中学到一些变化,但是相关站点会尝试向您出售课程,如果此链接不合适,请告诉我/或者将其删除: https : //codepen.io/SitePoint/pen/MmxQMG

 $(document).ready(function() { var div = $("#divToShowHide"); var pos = div.position(); $(window).scroll(function() { var windowpos = $(window).scrollTop(); console.log(pos.top) if (windowpos > pos.top && pos.top+500 > windowpos) { div.addClass("AfterScroll"); div.removeClass("BeforeScroll"); } else { div.addClass("BeforeScroll"); div.removeClass("AfterScroll"); } }); }); 
 body { height: 1200px; } #divToShowHide{ top:100px; position:fixed; } .BeforeScroll { height: 100px; width: 100%; display: none; } .AfterScroll { height: 100px; width: 100%; 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> 

I will give a an a example with scrollrevealjs 我将以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 然后在您的js文件中只是初始化库

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 https://github.com/jlmakes/scrollreveal.js

You can use some of the popular JS libraries such as: 您可以使用一些流行的JS库,例如:

http://scrollmagic.io/ http://scrollmagic.io/

https://scrollrevealjs.org/ https://scrollrevealjs.org/

Good luck :))) 祝好运 :)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM