gpt4 book ai didi

html - CSS - 在悬停固定子项时在父项内滚动

转载 作者:行者123 更新时间:2023-11-28 10:07:47 25 4
gpt4 key购买 nike

我有一个内部可滚动的元素。在其中,有可以滚动浏览的内容,还有一个 position: fixed; child 形成了一个侧边栏。

如果我将鼠标悬停在父级内的任何位置并滚动,父级的内容将按预期滚动。但是,如果我将鼠标悬停在仍在父边界框内的固定子项内并滚动,则不会:

<style>
body,
html {
margin: 0;
}

#container {
position: relative;
width: 100%;
height: 100vh;
overflow-y: auto;
}

#sidebar {
position: fixed;
right: 10px;
width: 100px;
top: 10px;
height: calc(100% - 20px);
background-color: red;
}
</style>

<div id='container'>

<div id='sidebar'>
<a href='https://google.com/'>Link to click</a>
When hovering here, scroll does NOT work.
</div>

<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
... and so on. When hovering here, scroll works.

</div>

我了解此功能的用途,但我有兴趣使滚动事件仍然从 position:fixed; 子级向上传播到其 overflow:auto; parent,允许用户滚动父元素,即使在悬停子元素时也是如此。这可能吗?

我已经尝试过使用 JavaScript 来监视子项上的滚动事件,然后将它们传递给父项,但是没有捕获到此类滚动事件(我想是因为元素本身没有任何内容可以在内部滚动)。我还尝试了父元素的各种配置(position:relative;, position:absolute;, position:fixed;) ,都无济于事。该问题仅在子项已修复时出现,但无法将其设置为绝对值。

最好,我想在没有 JavaScript 的情况下完成此操作,因此分解可能的解决方案(仅 HTML、HTML 和 CSS 以及仅 JavaScript)可能会有用。


我知道 this question ,但那里的第一个答案不起作用,因为在悬停固定子项时滚动不会触发滚动事件(对于子项或父项)。第二个答案也不可行,因为它会将 child 移出 parent ,这在我的情况下不是一个选项。


编辑: Harsh Karanpuria suggested我将 pointer-events: none; 添加到侧边栏。这确实实现了预期的结果,但它也使侧边栏中的元素不可点击,我宁愿不要这样做!在他们回答之后,我还再次尝试制作 sidebar pointer-events: all; 的子元素,但发现虽然这恢复了我点击它们的能力,但一次在悬停那个孙子时再次删除了可滚动的性质。

最佳答案

position: fixed;不是为此设计的;使用 position: sticky;

我在这里采取一个规范的立场:您不需要胡乱使用事件传播或类似的东西来实现您正在寻找的效果。过去可能是这种情况,但 CSS 有比 position: fixed; 更好的工具。 .

看看 CSS3 定位布局模块,这里是他们对固定定位的看法:

Fixed positioning, […] absolutely positions the box and affixes it to the viewport or page frame so that it is always visible.

这与你的解释不一致,强调我的:

However, if I hover inside the fixed child, which is still inside the bounding box of the parent, […]

从视觉上看,是的,固定的子项在其父项内......但只需向父项添加边距即可将其移开:

<style>
body,
html {
margin: 0;
}

#container {
/* <Changes> */
background-color: skyblue;
margin-top: 10vh;
/* </Changes> */
position: relative;
width: 100%;
height: 100vh;
overflow-y: auto;
}

#sidebar {
position: fixed;
right: 10px;
width: 100px;
top: 10px;
height: calc(100% - 20px);
background-color: red;
}
</style>

<div id='container'>

<div id='sidebar'>
<a href='https://google.com/'>Link to click</a>
When hovering here, scroll does NOT work.
</div>

<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
... and so on. When hovering here, scroll works.

</div>

我找不到在 position:fixed 上发生事件的原因在 W3C 规范中, child 不会像往常一样传播到 parent ,但情况仍然是 scroll事件传播到视口(viewport),而不是像往常一样冒泡到父元素。

来自 W3C CSS 定位布局模块级别 3 § 2:

[position: fixed:] Same as absolute, except the box is positioned and sized relative to a fixed positioning containing block (usually the viewport in continuous media, or the page area in paged media). The box’s position is fixed with respect to this reference rectangle: when attached to the viewport it does not move when the document is scrolled, and when attached to the page area is replicated on every page when the document is paginated. This positioning scheme is called fixed positioning and is considered a subset of absolute positioning.

position: fixed元素应该固定到视口(viewport),而不是它们的父元素。解决问题的自然方法是使用 position:sticky .

使用position:stickydisplay: flex

<style>
body,
html {
margin: 0;
}

#container {
/* <Changes> */
background-color: skyblue;
display: flex;
max-height: 40vh; /* Just to force scrolling in the demo. */
/* </Changes> */
position: relative;
width: 100%;
height: 100vh;
overflow-y: auto;
}

#sidebar {
position: sticky;
right: 0px;
top: 0px;
min-width: 100px;
background-color: red;
}
</style>

<div id='container'>
<!-- Changes -->
<div id="content">
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
... and so on. When hovering here, scroll works. Note this really long line is not overlapped by the sidebar.
</div>
<!-- /Changes -->
<div id='sidebar'>
<a href='https://google.com/'>Link to click</a>
When hovering here, scroll ALSO works.
</div>
</div>

我稍微更改了 HTML,将 <p>包含 div#content 的标签并重新排列内容和侧边栏的顺序。您可以更改 flex-direction并放弃重新排列。主旨是一样的:容器元素、内部内容、内部侧边栏。

此布局的效果与问题中呈现的不同:侧边栏不能与此布局中的内容重叠。如果这不是您想要的,您可以使用 display:grid 做得更好一点。 .

使用position:stickydisplay: grid

我们可以为侧边栏制作一个网格区域,像这样:

<style>
body,
html {
margin: 0;
}

#container {
/* <Changes> */
background-color: skyblue;
display: grid;
grid-template-columns: 1fr min-content;

max-height: 40vh; /* Just to force scrolling in the demo. */
/* </Changes> */
position: relative;
width: 100%;
height: 100vh;
overflow-y: auto;
}

#content {
grid-row: 1 / span 2;
grid-column: 1 / span 2;
}

#sidebar {
grid-row: 1;
grid-column: 2;
position: sticky;
right: 0px;
top: 0px;
min-width: 100px;
min-height: 110px;
background-color: red;
}

</style>

<div id='container'>
<!-- Changes -->
<div id="content">
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
... and so on. When hovering here, scroll works. Note this really long line is overlapped by the sidebar.
</div>
<!-- /Changes -->
<div id='sidebar'>
<a href='https://google.com/'>Link to click</a>
When hovering here, scroll ALSO works.
</div>
</div>

请注意,要使重叠起作用,我们需要手动设置 grid-columngrid-row#sidebar 上和 #content元素。

另见:

关于html - CSS - 在悬停固定子项时在父项内滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59151352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com