【发布时间】:2017-09-03 01:23:22
【问题描述】:
我希望当div 的滚动位置等于div 内的内容高度时显示警报消息。我已经编写了一些 jQuery,并且相信我非常接近能够做到这一点。
但是缺少一些东西。忽略滚动条div,后面会用到。谢谢
$(document).ready(function() {
var box1_height = $("#box1").height();
var scroll_pos = $("#box1").scrollTop();
if (box1_height == scroll_pos) {
alert("It's working");
}
});
* {
margin: 0;
padding: 0;
}
body {
overflow: hidden;
}
#background_1 {
background-image: url("http://placehold.it/350x150");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
position: relative;
}
.scroller {
width: 100%;
height: 100vh;
overflow: scroll;
}
.content {
width: 80%;
height: 80vh;
margin: 10vh auto;
background-color: rgba(0, 0, 0, 0.6);
overflow: scroll;
box-size: border-box;
}
<div id="background_1">
<div class="scroller">
<div id="box1" class="content">
content is here
</div>
</div>
</div>
这是CodePen Link。
【问题讨论】:
-
如果我理解正确,如果滚动位置在内容的末尾,您想触发某些功能?
-
是的,最终结果将触发滚动动画到下一部分
-
您需要在您的代码中添加一个
scroll事件处理程序,您的当前只会在页面加载后执行:example -
好的,谢谢。这让我后来省了很多麻烦
标签: javascript jquery html css