简体   繁体   English

当孩子被固定时,溢出隐藏在粘性父母身上

[英]Overflow hidden on sticky parent when child is fixed

I am building a website that has section panels slide up using position: sticky on scrolling but having issues with fixed elements inside the sticky panels not hiding outside of the section with overflow: hidden .我正在构建一个网站,该网站使用position: sticky on scrolling 将section面板向上滑动,但在sticky面板内的固定元素没有隐藏在section之外时出现问题overflow: hidden

How can I hide the Logo and Footer outside of each of their section so that they are not sitting over each other and instead on scroll the Logo and Footer appear as their intended colors?如何将徽标和页脚隐藏在其每个section之外,以使它们不会彼此重叠,而是在scroll时将徽标和页脚显示为预期的 colors?

Here is a simple snippet of the problem.这是问题的一个简单片段。 As you can see, on the sticky sections, I have applied overflow: hidden but this is ineffective.如您所见,在sticky部分,我应用了overflow: hidden ,但这无效。

 html { font-family: sans-serif; font-size: 20px; font-weight: 700; text-transform: uppercase; } *, ::after, ::before { box-sizing: border-box; } body { margin: 0; overflow-x: hidden; }.nav { position: fixed; width: 100%; top: 0; left: 0; padding: 24px 28px; display: flex; flex-direction: column; justify-content: space-between; }.footer { position: fixed; bottom: 0; right: 0; }.sticky { height: 100vh; width: 100vw; display: flex; justify-content: center; align-items: center; flex-direction: column; position: sticky; overflow: hidden; // Not working to hide and contain the logo and Footer in the section top: -1px; background-color: #ffffff; color: #000000; }.dark { background-color: #1e1e1e; color: #fff; }.image { color: #fff; background-image: url('https://images.unsplash.com/photo-1621864972187-063dae587c27?ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwyOHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'); background-size: cover; }
 <main> <div class="sticky"> <div class="nav"><svg height="30" width="200"> <text x="0" y="15" fill="black">Logo svg!</text> </svg></div> <p>Section Light</p> <div class="footer"><svg height="30" width="200" transform="rotate(90)"> <text x="0" y="15" fill="black">Footer!</text> </svg></div> </div> <div class="sticky dark"> <div class="nav"><svg height="30" width="200"> <text x="0" y="15" fill="white">Logo svg!</text> </svg></div> <p>Section Dark</p> <div class="footer"><svg height="30" width="200" transform="rotate(90)"> <text x="0" y="15" fill="white">Footer!</text> </svg></div> </div> <div class="sticky image"> <div class="nav"><svg height="30" width="200"> <text x="0" y="15" fill="white">Logo svg!</text> </svg></div> <p>Section with Image</p> <div class="footer"><svg height="30" width="200" transform="rotate(90)"> <text x="0" y="15" fill="white">Footer!</text> </svg></div> </div> <div class="sticky dark"> <div class="nav"><svg height="30" width="200"> <text x="0" y="15" fill="white">Logo svg!</text> </svg></div> <p>Section Dark</p> <div class="footer"><svg height="30" width="200" transform="rotate(90)"> <text x="0" y="15" fill="white">Footer!</text> </svg></div> </div> <div class="sticky"> <div class="nav"><svg height="30" width="200"> <text x="0" y="15" fill="black">Logo svg!</text> </svg></div> <p>Section Light</p> <div class="footer"><svg height="30" width="200" transform="rotate(90)"> <text x="0" y="15" fill="black">Footer!</text> </svg></div> </div> </main>

The website is built using Gatsby incase anyone has a JS solution.该网站是使用 Gatsby 构建的,以防任何人都有 JS 解决方案。 I was perhaps thinking of hiding each using a ref and applying a className .我可能正在考虑使用ref隐藏每个并应用className Although I am sure there is a simpler solution out there.虽然我确信那里有一个更简单的解决方案。

Not sure whether you need separate content for the nav and footer per section but this answer only applies if you want the same header and footer content to appear over all sections and just change color.不确定您是否需要每个部分的navfooter单独的内容,但此答案仅适用于您希望相同的 header 和页脚内容出现在所有部分并且只是更改颜色的情况。

It's possible usingmix-blend-mode and the support is good for modern browsers.可以使用mix-blend-mode并且对现代浏览器的支持很好。 The spec says that Safari doesn't work "On SVG elements" but it seems to work fine on Safari in the demo below, maybe because it's applied to the SVG's parent.规范说 Safari “在 SVG 元素上”不起作用,但它似乎在下面的演示中的 Safari 上工作正常,可能是因为它应用于 SVG 父级。

I pulled the nav and footer out of each section and made one copy of each at the top.我将导航和页脚从每个部分中拉出,并在顶部分别制作了一份副本。 Then I applied mix-blend-mode: difference;然后我应用mix-blend-mode: difference; to both of them and made the fill color of the SVG elements white.到他们两个,并将 SVG 元素的填充颜色设为白色。 Then I added mix-blend-mode: screen;然后我添加了mix-blend-mode: screen; to the .dark class which makes it so that whenever the white SVG elements with difference on their parent container pass over it the color of the SVG elements will change to white..dark class 这使得每当白色 SVG 元素在其父容器上有difference时通过它, SVG 元素的颜色将变为白色。 This will not be very useful if you're looking to support IE or if you want different headers and footers by section but in the event that you just want what you had in your code demo to work as you explained then it should be fine for modern browsers.如果您希望支持 IE,或者如果您希望按部分使用不同的页眉和页脚,那么这将不是很有用,但是如果您只希望代码演示中的内容按照您的解释工作,那么它应该没问题现代浏览器。

If you do need varying content per section then it will most likely require Javascript since a fixed element will not respect a relative positioned parent element's rules or will not act like a window fixed element on scroll when using the translateZ hack for instance, good luck!如果您确实需要每个部分不同的内容,那么它很可能需要 Javascript 因为固定元素不会遵守相对定位的父元素的规则,或者在使用translateZ hack时不会像 window 滚动上的固定元素一样,祝你好运!

 html { font-family: sans-serif; font-size: 20px; font-weight: 700; text-transform: uppercase; } *, ::after, ::before { box-sizing: border-box; } body { margin: 0; overflow-x: hidden; }.nav { position: fixed; width: 100%; top: 0; left: 0; padding: 24px 28px; display: flex; flex-direction: column; justify-content: space-between; z-index: 99; mix-blend-mode: difference; }.footer { position: fixed; bottom: 0; right: 0; z-index: 99; mix-blend-mode: difference; }.sticky { height: 100vh; width: 100vw; display: flex; justify-content: center; align-items: center; flex-direction: column; position: relative; background-color: #ffffff; color: #000000; }.sticky p { position: sticky; top: 50%; transform: translateY(-50%) }.dark { background-color: #1e1e1e; color: #fff; mix-blend-mode: screen; }
 <div class="nav"><svg height="30" width="200"> <text x="0" y="15" fill="#fff">Logo svg!</text> </svg></div> <div class="footer"><svg height="30" width="200" transform="rotate(90)"> <text x="0" y="15" fill="#fff">Footer!</text> </svg></div> <main> <div class="sticky"> <p>Section Light</p> </div> <div class="sticky dark"> <p>Section Dark</p> </div> <div class="sticky"> <p>Section Light</p> </div> <div class="sticky dark"> <p>Section Dark</p> </div> <div class="sticky"> <p>Section Light</p> </div> </main>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM