html+ JavaScript卷轴检测不起作用
我似乎无法让滚动事件用于此布局。我被困,我想知道你们是否可以帮助我。我要做的是检测何时滚动的内容=“ mycontent”的内容div是在滚动。我在做什么错?
提前致谢。
<!DOCTYPE html>
<html>
<head>
<script>
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
console.log("scrollFunction");
}
console.log(document.getElementById("myContent")); //dont know why it cannot find this arrrgghhh !!!
</script>
</head>
<body style="width: 100%;
height: 100%;
top: 0;
left: 0;
margin: 0;
padding: 0;
position: absolute;">
<!--container-->
<div style="border: 1px solid red;
width: 100%;
height: 100%;
position: fixed;
overflow: auto;
overflow-x: hidden;">
<!--header-->
<div style="border: 1px solid green;
width: 100%;
height: 75px;
top: 0;
position: sticky;
margin: 0 auto;
align-items: center;">
</div>
<!--content-->
<div id="myContent"
onscroll="scrollFunction();"
style="border: 1px solid blue;
width: 100%;
height: auto;
margin: 0 auto;">
<!--content 1-->
<div style="width: 100%; height: 1000px;">
</div>
</div>
<!--footer-->
<div style="border: 1px solid pink;
width: 100%;
height: 150px;
margin: 0 auto;">
</div>
</div>
</body>
任何帮助都将受到赞赏。 提前
感谢
I cannot seem to get scroll event to work for this layout. I am stuck and I was wondering if you guys can help me out. What I am trying to do is to detect when the content div, which has id="myContent", is scrolling. What am I doing wrong?
Thanks in advance.
<!DOCTYPE html>
<html>
<head>
<script>
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
console.log("scrollFunction");
}
console.log(document.getElementById("myContent")); //dont know why it cannot find this arrrgghhh !!!
</script>
</head>
<body style="width: 100%;
height: 100%;
top: 0;
left: 0;
margin: 0;
padding: 0;
position: absolute;">
<!--container-->
<div style="border: 1px solid red;
width: 100%;
height: 100%;
position: fixed;
overflow: auto;
overflow-x: hidden;">
<!--header-->
<div style="border: 1px solid green;
width: 100%;
height: 75px;
top: 0;
position: sticky;
margin: 0 auto;
align-items: center;">
</div>
<!--content-->
<div id="myContent"
onscroll="scrollFunction();"
style="border: 1px solid blue;
width: 100%;
height: auto;
margin: 0 auto;">
<!--content 1-->
<div style="width: 100%; height: 1000px;">
</div>
</div>
<!--footer-->
<div style="border: 1px solid pink;
width: 100%;
height: 150px;
margin: 0 auto;">
</div>
</div>
</body>
Any help is appreciated.
Thanks in advance
Best regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,当前垂直滚动条没有出现在
div
上,该具有onScroll
侦听器定义但在其父母上。为了查看该div上的滚动条,您必须与Overflow-y:auto
一起明确定义其高度
。还要确保在父div
上定义溢出:隐藏
。请查看您的修订代码,看看它是如何工作的。
The problem is that currently the vertical scrollbar does not appear on the
div
that has theonscroll
listener defined but on its parent. In order to see the scrollbar on that div, you must explicitly define itsheight
together withoverflow-y: auto
. Make sure also to defineoverflow: hidden
on the parentdiv
.Please take a look at your amended code and see how it works.