可以将滚动事件的上下文设置为 `document.body` 而不是 `window` 吗?

huangapple go评论61阅读模式
英文:

Is it possible to set the context of scroll event to `document.body` instead of `window`?

问题

背景

在阅读mdn页面时,我注意到左右侧边栏的滚动条嵌套在自身内部。但内容区块的滚动条在全局上下文中。

可以将滚动事件的上下文设置为 `document.body` 而不是 `window` 吗?

当我检查DOM树时,我看到bodyhtml标签上没有定义高度属性。因此,实质上,在内容区域和最顶层窗口对象之间没有具有固定高度(可滚动容器)的元素。
滚动容器的示例

问题

这使得window成为内容区域的溢出容器。
进一步证实这一点,我为windowdocument.body都添加了scroll事件监听器,结果只有window事件被触发

window.addEventListener("scroll",(e)=>console.log("window scrolling")) 

document.body.addEventListener("scroll",(e)=>console.log("body scrolling"))

但我希望滚动的上下文不是window,而是document.body。而且应该从body标签触发滚动事件,而不是窗口。

我做了什么

然后我为body标签添加了height属性。

height: 100vh

然后再次为其添加了滚动事件监听器。但是它没有触发,我不明白为什么?

为具有小于其子元素值的父元素添加height属性应该使其可滚动,对吧?

我可能漏掉了一些基本的东西,但不知道是什么?
请帮忙,谢谢!

参考-

英文:

Backdrop

While I was reading mdn page, I noticed that the left and right sidebars have their scrollbars contained within themselves. But the content area block has its scrollbar in the global context.

可以将滚动事件的上下文设置为 `document.body` 而不是 `window` 吗?

When I inspected the DOM tree, I saw that the body and html tags doesn't have height property defined on them. So essentially, their were no elements with fixed height (scrollable container) between the content area and the topmost window object.
An example of scrollable container

Problem

So this makes window the overflow container for the content area.
Further confirming this, I added scroll event listeners to both window and document.body and it turns out that only window event is fired

window.addEventListener("scroll",(e)=>console.log("window scrolling")) 

document.body.addEventListener("scroll",(e)=>console.log("bodyy scrolling"))

But I want the context for scroll should not be window but document.body. And that should fire scroll event from body tag and not window.

What I did

I then added height property to the body tag.

height: 100vh

and then added scroll event listener to it again. But It didn't fired, and I am not able to understand why it didn't?

Adding a height property to a parent which has a value less than its children should make it scrollable right?

I might be missing something fundamental but don't know what?
Please help, Thanks!

ref-

答案1

得分: 3

确保在<body><html>元素上将overflow属性设置为除了visible之外的其他值,因为涉及Overflow Viewport Propagation

要出现滚动条,<body>的高度必须额外小于其包含的内容的高度:

window.addEventListener("scroll", () => console.log("window scrolled"));
document.body.addEventListener("scroll", () => console.log("body scrolled"));
html {overflow-y: hidden}
body {
  margin: 0;
  height: 100dvh;
  overflow-y: scroll;
}
main {
  height: 200%;
  background-color: red;
}
<body>
  <main></main>
</body>
英文:

Make sure to set the overflow property on the &lt;body&gt;, and on the &lt;html&gt; element to something other than visible because of Overflow Viewport Propagation.

For a scrollbar to appear, &lt;body&gt;'s height must additionally be less than the content's it contains:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

window.addEventListener(&quot;scroll&quot;, () =&gt; console.log(&quot;window scrolled&quot;));
document.body.addEventListener(&quot;scroll&quot;, () =&gt; console.log(&quot;body scrolled&quot;));

<!-- language: lang-css -->

html {overflow-y: hidden}
body {
  margin: 0;
  height: 100dvh;
  overflow-y: scroll;
}
main {
  height: 200%;
  background-color: red;
}

<!-- language: lang-html -->

&lt;body&gt;
  &lt;main&gt;&lt;/main&gt;
&lt;/body&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月25日 19:15:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定