简体   繁体   English

如何完全隐藏页面外边距的元素

[英]How to completely hide an element with margin outside the page

The whole code works like this: when you enter the page, it's empty, but when u scoll down, content shows up.整个代码是这样工作的:当你进入页面时,它是空的,但是当你向下滚动时,内容会显示出来。

I have a problem with hiding an element outside the page.我在隐藏页面外的元素时遇到问题。 The parent has position relative.父级具有相对位置。 THe object has position absolute and left: -100% property.该对象具有绝对位置和左:-100% 属性。 It was completely hidden before I added a margin to it.在我为其添加边距之前,它已完全隐藏。 I need margin to seperate divs from each other.我需要保证金来将 div 彼此分开。

This is my code:这是我的代码:

 document.addEventListener("DOMContentLoaded", function() { window.onscroll = function() { var y = window.scrollY; var x = document.querySelector(".main-inner2-container").getBoundingClientRect().top; var z = window.innerHeight / 2; if (z > x) { document.querySelector(".main-inner2-content1").style.animation = "FadeIn 0.5s linear 0s 1 forwards"; document.querySelector(".main-inner2-container2-inner1-content").style.animation = "MoveInFromLeft 0.5s ease-out 0s 1 forwards"; document.querySelector(".main-inner2-container2-inner2-content").style.animation = "MoveInFromRight 0.5s ease-out 0s 1 forwards"; document.querySelector(".main-inner2-container3-inner1-content").style.animation = "MoveInFromLeft 0.5s ease-out 0s 1 forwards"; document.querySelector(".main-inner2-container3-inner2-content").style.animation = "MoveInFromRight 0.5s ease-out 0s 1 forwards"; } }; });
 * { margin: 0; padding: 0; box-sizing: border-box; } .main { display: block; margin: auto; height: 1000px; width: 100%; background-color: grey; } .main-inner1 { display: block; margin: auto; height: 600px; width: 100%; background-color: orange; } .main-inner2 { display: block; margin: auto; width: 100%; background-color: green; padding: 12px; } .main-inner2-container { display: block; position: relative; height: 50px; width: 200px; overflow: hidden; margin: auto; z-index: 9998; margin: 12px auto 12px auto; } .main-inner2-content1 { display: block; position: absolute; right: 100%; margin: auto; height: 100%; width: 100%; background-color: blue; } .main-inner2-container2 { display: flex; justify-content: space-around; } .main-inner2-container2-inner1 { flex-grow: 1; margin: 12px; height: 300px; position: relative; } .main-inner2-container2-inner1-content { position: absolute; width: 100%; height: 300px; left: -100%; background-color: red; } .main-inner2-container2-inner2 { flex-grow: 2; margin: 12px; height: 300px; position: relative; } .main-inner2-container2-inner2-content { position: absolute; width: 100%; height: 300px; right: -100%; background-color: red; } .main-inner2-container3 { display: flex; justify-content: space-around; } .main-inner2-container3-inner1 { flex-grow: 2; margin: 12px; height: 300px; position: relative; } .main-inner2-container3-inner1-content { position: absolute; width: 100%; height: 300px; left: -100%; background-color: red; } .main-inner2-container3-inner2 { flex-grow: 1; margin: 12px; height: 300px; position: relative; } .main-inner2-container3-inner2-content { position: absolute; width: 100%; height: 300px; right: -100%; background-color: red; } @keyframes FadeIn { 0% { right: 100%; } 100% { right: 0; } } @keyframes MoveInFromLeft { 0% { left: -100%; } 100% { left: 0; } } @keyframes MoveInFromRight { 0% { right: -100%; } 100% { right: 0; } }
 <div class="main"> <div class="main-inner1"></div> <div class="main-inner2"> <div class="main-inner2-container"> <div class="main-inner2-content1"></div> </div> <div class="main-inner2-container2"> <div class="main-inner2-container2-inner1"> <div class="main-inner2-container2-inner1-content"></div> </div> <div class="main-inner2-container2-inner2"> <div class="main-inner2-container2-inner2-content"></div> </div> </div> <div class="main-inner2-container3"> <div class="main-inner2-container3-inner1"> <div class="main-inner2-container3-inner1-content"></div> </div> <div class="main-inner2-container3-inner2"> <div class="main-inner2-container3-inner2-content"></div> </div> </div> </div> </div>

I hope I described my problem well and sorry for the class names.我希望我能很好地描述我的问题,并为班级名称感到抱歉。 Code for learning :P学习代码:P

The way I solved your problem was to use CSS Variables to save the size of the padding/margin towards the viewport-edge, and increase the offset of the elements by that much more.我解决您的问题的方法是使用CSS 变量来保存朝向视口边缘的填充/边距的大小,并将元素的偏移量增加更多。 And to add gaps between the elements you can use the gap -property (provided you are using display: flex or display: grid ).要在元素之间添加间隙,您可以使用gap属性(前提是您使用的是display: flexdisplay: grid )。

Sidenotes旁注
First off, when styling with JS, you should try to avoid using inline styling ( element.style ) because that overrides all stylings from your CSS, which might be unwanted and thus hard to debug because your CSS file itself is correct.首先,当使用 JS 进行样式设置时,您应该尽量避免使用内联样式 ( element.style ),因为它会覆盖 CSS 中的所有样式,这可能是不需要的,因此很难调试,因为您的 CSS 文件本身是正确的。

Secondly, you seem to confuse CSS Classes and IDs, because you are using your classes effectively as IDs.其次,您似乎混淆了 CSS 类和 ID,因为您将类有效地用作 ID。 You should try to bundle similar stylings into one class and apply that class to multiple elements (eg see .container or .content in my code below).您应该尝试将类似的样式捆绑到一个类中,并将该类应用于多个元素(例如,请参阅下面代码中的.container.content )。

And also, there are more modern ways to style your HTML, like using relative units (eg fr, em, rem) instead of fixed units (px), or using CSS Flexbox or CSS Grid to lay out your elements instead of using the left -/ right -properties (which are still useful, but in my opinion should most of the times be exchanged with one of the two mentioned display-styles).而且,还有更现代的方式来设置 HTML 样式,例如使用相对单位(例如 fr、em、rem)而不是固定单位(px),或者使用CSS FlexboxCSS Grid来布局元素而不是使用left -/ right -properties(仍然有用,但在我看来,大多数时候应该与提到的两种显示样式之一进行交换)。

And instead of using the animation , I used the transition -property since we are trying to "animate" it linearly (or any of the other available timing functions), better said we are not using the keyframes feature effectively.而不是使用animation ,我使用了transition属性,因为我们试图线性地“动画”它(或任何其他可用的计时函数),最好说我们没有有效地使用关键帧功能。

Also, there already is a designated <main> -tag available, which apart from saving you an id also saves you the hassle of making your HTML semantically correct.此外,已经有一个指定的<main> -tag 可用,它除了为您保存一个 id 之外,还可以为您省去使 HTML 语义正确的麻烦。 Try to use the correct HTML-tags when possible (which does not mean that you should avoid the <div> -tag!).尝试使用正确的HTML标签时可能(这并不意味着你应该避免<div> -标签!)。

PS聚苯乙烯
I took the time to rewrite most of your code the way I would have written it.我花时间按照我会写的方式重写了你的大部分代码。

This does in no way mean that mine is more correct than yours.这绝不意味着我的比你的更正确。 Coding is art, and techniques of different artists are in no way superior to the others (well... sometimes).编码是一门艺术,不同艺术家的技术绝不比其他人优越(嗯……有时)。 It's all just personal preference.这一切只是个人喜好。

 window.addEventListener("load", function() { window.addEventListener("scroll", function() { let mainContentTop = document.querySelector("main > div") .getBoundingClientRect().top; const vh = window.innerHeight; if (vh / 2 > mainContentTop) { for (let el of document.querySelectorAll("main *.move-in")) { el.classList.remove("out-left"); el.classList.remove("out-right"); } } }); });
 body { margin: 0; display: flex; flex-flow: column; overflow-x: hidden; } main { display: flex; flex-flow: column; } main::before { content: ""; height: 100vh; background: orange; } main > div { --main-div-padding: 0.8em; padding: var(--main-div-padding); display: flex; flex-flow: column; gap: var(--main-div-padding); background-color: green; } main header { align-self: center; display: flex; height: 3.125rem; width: 12.5rem; overflow: hidden; } main header > div { width: 100%; transform: translate(-100%); transition: transform 0.5s; background-color: blue; } main header > div.move-in.out-left {transform: translate(-100%)} main .container { height: 18.75rem; display: grid; gap: var(--main-div-padding); } main .content { width: 100%; background: red; } main .container:nth-of-type(odd) {grid-template-columns: 1fr 2fr} main .container:nth-of-type(even) {grid-template-columns: 2fr 1fr} /* Tool-classes */ .move-in { transform: translateX(0); transition: transform 0.5s; } main > div *.move-in.out-left { transform: translateX(calc(-100% - var(--main-div-padding))); } main > div *.move-in.out-right { transform: translateX(calc(100% + var(--main-div-padding))); }
 <main> <div> <header> <div class="move-in out-left"></div> </header> <div class="container"> <div class="content move-in out-left"></div> <div class="content move-in out-right"></div> </div> <div class="container"> <div class="content move-in out-left"></div> <div class="content move-in out-right"></div> </div> </div> </main>

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

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