当我向下滚动网页时,我看到了内容出现的网站。我有这段代码,但它不起作用。你能指导并给予适当的解释吗?
$(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>
原文由 Varun Jain 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果你也想制作一些动画,我会建议你 AOS
这是一个 Animate On Scroll Library,你可以让内容在向下滚动时出现
如何使用:
在 HTML 标签中添加“data-aos=“animation name”” 就可以了:
添加后:
在头部并添加:
在 body 标签结束之前。
一个简单的例子: https ://codepen.io/karenmio/pen/KxGewG
您可以从中学到一些变化,但相关网站确实会尝试向您出售课程,如果此链接不正确,请告诉我/或将其删除:https: //codepen.io/SitePoint/pen/MmxQMG