简体   繁体   English

嗨,我需要在滚动时不显示卡片。 导航栏的 z-index=999

[英]Hi, I need the card not to be shown while scrolling. The navigation bar has the z-index=999

Here is the code for the navbar:这是导航栏的代码:

display: flex;
width: 100%;
height: 100px;
justify-content: space-between;
align-items: center;
box-shadow: 2px 2px 15px #616161;
position: sticky;
top: 0;
z-index: 1000;

and card:和卡:

background: #fff;
width: 80%;
border-radius: 15px;
margin: 20px 0;
max-height: 190px;
box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;

( https://i.stack.imgur.com/tDbyM.png ) ( https://i.stack.imgur.com/tDbyM.png )

I expect the navbar to be displayed over the card.我希望导航栏显示在卡片上。

@chrslg's suggestion is correct. @chrslg 的建议是正确的。 You have to add background-color to your navbar.您必须向导航栏添加background-color Like this:像这样:

.your_navbar_class {
    display: flex;
    width: 100%;
    height: 100px;
    justify-content: space-between;
    align-items: center;
    box-shadow: 2px 2px 15px #616161;
    position: sticky;
    top: 0;
    z-index: 1000;
    background-color: white; /* Added */
}

Just to demonstrate my point (both to you and to myself:-)), see this snipped.只是为了证明我的观点(对你和我自己:-)),请看这个片段。

Your div.nav is over, as you wanted.你的 div.nav 已经结束,如你所愿。 But we see the content through.但是我们看透了内容。 If you click on "toggle background" button, you can see that it is indeed, over.如果你点击“切换背景”按钮,你可以看到它确实结束了。

 let hasback=false; document.getElementById("bt").addEventListener("click", function(){ let nav=document.querySelector("div.nav"); if(hasback) nav.style.backgroundColor='#00000000'; else nav.style.backgroundColor='#fff'; hasback=;hasback; });
 div.nav { display: flex; width: 100%; height: 100px; justify-content: space-between; align-items: center; box-shadow: 2px 2px 15px #616161; position: sticky; top: 0; z-index: 1000; } div.content{ background: #fff; width: 80%; border-radius: 15px; margin: 20px 0; max-height: 190px; box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px; }
 <div class="nav"> FOO BAR </div> <div class="content"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> <button id=bt>Toggle Background</button>

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

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