gpt4 book ai didi

html - 网页上的悬停效果覆盖下拉导航栏

转载 作者:行者123 更新时间:2023-11-27 23:41:42 31 4
gpt4 key购买 nike

我有一个网页只使用 html 和 CSS(还没有 JavaScript)。该网页包含一个简单的固定位置标题,每个类别都有悬停下拉菜单。网页内容本身以“图 block ”的形式排列,这些图 block 在悬停时产生效果以强调它们(并链接到其他页面)。我将附上一张图片以更好地解释。下拉菜单在网页的最顶部工作正常,但是如果下拉菜单落在其中一个磁贴的顶部,下拉菜单将消失并触发磁贴悬停效果。

我已确保下拉列表的所有部分的 z-index 都大于图 block 。

下拉 HTML:

<nav class="navigation">
<ul>
<li class=dropdown>
<a href="catagory1.html" class="dropdown-head">catagory1</a>
<div class="dropdown-content">
<a href="../content1.html">content1</a>
<a href="/content2.html">content2</a>
<a href="../content3.html">content3</a>
</div>
</li>
<li class="dropdown">
<a href="catagory2.html">catagory2</a>
<div class="dropdown-content">
<a href="content4">content4</a>
<a href="content5.html">content5</a>
</div>
</li>
</ul>
</nav>

页面内容 HTML(单个图 block 示例,代码重复):

<a href="content6.html" class="tile">
<img src="image1.jpg>
<div class="container">
<div class="overlay"></div> /* overlay that animates on hover */
/* content for the tile */
</div>
</a>

相关CSS:

navigation {
position: fixed;
}
.navigation li {
display: inline-block;
margin: 0 5%;
}

.dropdown {
position: relative;
display: inline-block;
z-index: 20;
}

.dropdown-content {
display: none;
position: absolute;
}

.dropdown-content a {
display: inline-block;
position: relative;
}

.dropdown-content a:hover {
background-color: blue;
color: white;
}

.dropdown:hover .dropdown-content{
display: block;
}

.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
z-index: 17;
}

.tile:hover .overlay {
opacity: 0.1;
}

图片 1: Top of page(Works Fine)图片 2: Scrolled down(Hovering over Content 3 will close dropdown)

无论是否触发悬停效果,我都希望下拉菜单保留在图 block 上方。我不确定这是否需要 JavaScript。感谢您的帮助!

编辑:添加片段

/* General Rules */

* {
margin: 0;
padding: 0;
border: none;
font-family: "DM Serif Text", serif;
}
body {
background-color: #1b1d35;
color: whitesmoke;
max-width: 100%;
overflow-x: hidden;
margin: 0 auto;
}

ul {
margin: inherit;
padding: none;
border: none;
list-style: none;
text-align: center;

}

li {
margin: inherit;
padding: none;
border: none;
}


a {
color: whitesmoke;
font-size: 2rem;
display: inline-block;
width: auto;
height: auto;
text-decoration: none;
margin: 0 auto;
transition-duration: 0.3s;


}

a:not(p):hover {
color: #38b6ff;
}

header {
position: fixed;
z-index: 10;
background-color: #7e7d7d;
width: 100%;
height: 10rem;
margin: inherit;
padding: none;
border: none;
}

#head-logo {
position: absolute;
width: 15%;
height: auto;
}

.navigation {
position: fixed;
z-index: 20;
width: 100%;
margin: 0 auto;
border: none;
padding: none;
top: 5rem;
}

.navigation li {
display: inline-block;
margin: 0 5%;
}

.dropdown {
position: relative;
display: inline-block;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #7e7d7d;
box-shadow: 0 1rem 2rem 0 rgba(0,0,0,0.2);
}

.dropdown-content a {
font-size: 1.5rem;
color: whitesmoke;
text-decoration: none;
text-align: left;
display: inline-block;
width: 17.5rem;
}

.dropdown-content a:hover {background-color: #7e7d7dd8; box-shadow: 1rem rgb(0,0,0); color: rgb(56, 182, 255);}
.dropdown:hover .dropdown-content{display: block;}

.sidebar {
position: fixed;
top: 17rem;
margin-left: 0.1%;
text-align: left;
}
.sidebaritem{
font-size: 1rem;
margin-top: 1rem;
margin-left: 0;
text-align: left;
}

h3 {
font-size: 1.5rem;
margin: 1rem auto;
}


#main {
position: relative;
top: 12rem;
width: 65%;
margin: inherit;
}

img {
width: 100%;
height: auto;
}

.row {
height: 20rem;
}
.column {
float: left;
width: 31%;
padding: 1%;
position: relative;
}

#bigtile {
width: 64%;
padding: 1%;
}



.container {
position: relative;
}
/* Things that apply to all tiles */
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: 0.5s ease;
background-color: #38b6ff;
z-index: 17;
}




/* Style for tiles where pictures take up full tile */
.pic-tile {
border-top: 5px solid #38b6ff;
}

.pic-tile .container img {
height:100%;
width:100%;
}

.pic-tile .container h2{
position: absolute;
left: 2%;
bottom: 2rem;
z-index: 7;
}

.pic-tile .container h3 {
position: absolute;
left: 2%;
bottom: 0;
z-index: 7;

}

.pic-tile:hover .overlay {
opacity: 0.1;
}

/* Style for regular tiles */
.tile img {
height: 50%;
width: 100%;
top: 0;
}

.tile {
background-color: gray;
bottom: 1%;
text-align: end;
position: relative;
width: 100%;
height: 100%;
border-top: 5px solid #38b6ff;
}

.tile .container{
font-size: 1rem;
position: relative;
margin-bottom: 1%;
margin-left: 1%;
}

.tile .container p{
font-size: 1rem;
position: relative;
text-align: end;
color: whitesmoke;
}

.tile:hover .overlay {
opacity: 0.1;
}
<html>
<body>
<!-- <img> insert logo here -->
<header>
<nav class="navigation">
<ul>
<li class=dropdown>
<a href="cat1.html" class="dropdown-head">Catagory1</a>
<div class="dropdown-content">
<a href="cont1.html">content1</a>
<a href="cont2.html">content2</a>
<a href="content3.html">content3</a>
</div>
</li>
<li class="dropdown">
<a href="cat2.html">Catagory2</a>
<div class="dropdown-content">
<a href="content4.html">content4</a>
<a href="content5.html">content5</a>
</div>
</li>
<li><a href="cat3">Catagory3</a></li>
</ul>
</nav>
</header>


<div id="main">


<div class="row">

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

</div>


<div class="row">

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

</div>

<div class="row">

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

</div>



</div>

</div>
</body>
</html>

最佳答案

这个问题可以通过将headerz-index改为20来解决。即使.dropdown的z-index class 为 20,header 元素的 z-index 为 10。

虽然 z-index 不是继承的,但需要考虑堆叠上下文。例如,如果兄弟元素 B 的 z-index 大于兄弟元素 A 的 z-index,则 B 将是在 A 之上。如果 A 的子元素的 z-index 高于元素 B 的子元素,则 B 仍将显示在 A 上方 前提是 B 的 z-index 大于 A

有关堆栈上下文的更多信息,请查看此 Post .

/* General Rules */

* {
margin: 0;
padding: 0;
border: none;
font-family: "DM Serif Text", serif;
}
body {
background-color: #1b1d35;
color: whitesmoke;
max-width: 100%;
overflow-x: hidden;
margin: 0 auto;
}

ul {
margin: inherit;
padding: none;
border: none;
list-style: none;
text-align: center;

}

li {
margin: inherit;
padding: none;
border: none;
}


a {
color: whitesmoke;
font-size: 2rem;
display: inline-block;
width: auto;
height: auto;
text-decoration: none;
margin: 0 auto;
transition-duration: 0.3s;


}

a:not(p):hover {
color: #38b6ff;
}

header {
position: fixed;
z-index: 20;
background-color: #7e7d7d;
width: 100%;
height: 10rem;
margin: inherit;
padding: none;
border: none;
}

#head-logo {
position: absolute;
width: 15%;
height: auto;
}

.navigation {
z-index:20;
position: fixed;
width: 100%;
margin: 0 auto;
border: none;
padding: none;
top: 5rem;
}

.navigation li {
display: inline-block;
margin: 0 5%;
}

.dropdown {

position: relative;
display: inline-block;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #7e7d7d;
box-shadow: 0 1rem 2rem 0 rgba(0,0,0,0.2);
}

.content1{


}
.dropdown-content a {

font-size: 1.5rem;
color: whitesmoke;
text-decoration: none;
text-align: left;
display: inline-block;
width: 17.5rem;
}

.dropdown-content a:hover {background-color: #7e7d7dd8; box-shadow: 1rem rgb(0,0,0); color: rgb(56, 182, 255);}
.dropdown:hover .dropdown-content{display: block;}

.sidebar {
position: fixed;
top: 17rem;
margin-left: 0.1%;
text-align: left;
}
.sidebaritem{
font-size: 1rem;
margin-top: 1rem;
margin-left: 0;
text-align: left;
}

h3 {
font-size: 1.5rem;
margin: 1rem auto;
}


#main {
position: relative;
top: 12rem;
width: 65%;
margin: inherit;
}

img {
width: 100%;
height: auto;
}

.row {
height: 20rem;
}
.column {
float: left;
width: 31%;
padding: 1%;
position: relative;
}

#bigtile {
width: 64%;
padding: 1%;
}



.container {
position: relative;
}
/* Things that apply to all tiles */
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: 0.5s ease;
background-color: #38b6ff;
z-index: 17;
}




/* Style for tiles where pictures take up full tile */
.pic-tile {
border-top: 5px solid #38b6ff;
}

.pic-tile .container img {
height:100%;
width:100%;
}

.pic-tile .container h2{
position: absolute;
left: 2%;
bottom: 2rem;
z-index: 7;
}

.pic-tile .container h3 {
position: absolute;
left: 2%;
bottom: 0;
z-index: 7;

}

.pic-tile:hover .overlay {
opacity: 0.1;
}

/* Style for regular tiles */
.tile img {
height: 50%;
width: 100%;
top: 0;
}

.tile {
background-color: gray;
bottom: 1%;
text-align: end;
position: relative;
width: 100%;
height: 100%;
border-top: 5px solid #38b6ff;
}

.tile .container{
font-size: 1rem;
position: relative;
margin-bottom: 1%;
margin-left: 1%;
}

.tile .container p{
font-size: 1rem;
position: relative;
text-align: end;
color: whitesmoke;
}

.tile:hover .overlay {
opacity: 0.1;
}
<!DOCTYPE html >
<html>
<body>
<!-- <img> insert logo here -->
<header>
<nav class="navigation">
<ul>
<li class=dropdown>
<a href="cat1.html" class="dropdown-head">Catagory1</a>
<div class="dropdown-content">
<a href="cont1.html" class="content1">content1</a>
<a href="cont2.html">content2</a>
<a href="content3.html">content3</a>
</div>
</li>
<li class="dropdown">
<a href="cat2.html">Catagory2</a>
<div class="dropdown-content">
<a href="content4.html">content4</a>
<a href="content5.html">content5</a>
</div>
</li>
<li><a href="cat3">Catagory3</a></li>
</ul>
</nav>
</header>


<div id="main">


<div class="row">

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

</div>


<div class="row">

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

</div>

<div class="row">

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

<div class="column">
<a href="cont8.html" class="tile">
<div class="overlay"></div>
<img class="image" src="img.png">
<div class="container">
<h2>Hello World</h2>
</div>
</a>
</div>

</div>



</div>

</div>
</body>
</html>

关于html - 网页上的悬停效果覆盖下拉导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57008674/

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