javascript - show a div when the top of the screen touches another div
问题描述
I'm trying to make a div appear and disappear by using jquery with scroll. I want it to appear when the div called "contingut-pagina" appears on the screen, the code shown below is working, but it changes the classe when it touches the bottom of the screen and I want it to do so when the div touches the top . It's possible? I copy the current code here:
function viewportCustom() {
$(window).scroll(function() {
if($(window).scrollTop() > ($(".contingut-pagina").position().top - $(window).height())) {
$(".block-simplified-social-share").css("opacity", "1");
}
if($(window).scrollTop() < ($(".contingut-pagina").position().top - $(window).height())) {
$(".block-simplified-social-share").css("opacity", "0");
}
})
}
解决方案
我找到了一个使用 .offset() 的解决方案,现在它可以像我想要的那样工作。谢谢大家
function viewportCustom() {
$(window).scroll(function() {
if ($(window).scrollTop() >= $('.contingut-pagina').offset().top){
$('.block-simplified-social-share').css("opacity", "1");
}
if ($(window).scrollTop() < $('.contingut-pagina').offset().top){
$('.block-simplified-social-share').css("opacity", "0");
}
});
}
推荐阅读
- python - 为什么我们把整数放在第三位?
- java - Java/JTable - 改变 JTable 滚动条的大小
- encryption - 在经典 ASP 中验证 Stripe Webhook 签名
- xpath - xpath 查找前一个元素
- php - 生成为 Laravel 作业队列创建表的迁移?
- python - 从股票数据生成烛台
- github - 如何以特定的时间间隔执行位于 GitHub 存储库中的代码?
- c# - C# ASP.NET MVC & Entity Framework 6 抛出错误“未找到存储过程”
- typescript - 无法解析子目录中的模块
- postgresql - 更新时引用无效,那么,如何更新呢?