.dropbtn按钮具有附加的样式,以便将鼠标悬停在其上时,通过向该元素添加不透明度来显示.dropdown-content div。但是,将鼠标悬停在显示.dropdown-content的按钮下方的位置上似乎触发了仅当我将鼠标悬停在.dropbtn上时才发生的悬停。为什么会这样呢?
有一些“解决方案”,例如将overflow:hidden添加到.dropdown,然后将其与悬停一起添加回去,但这会影响我附加到.dropdown-content的转换。将display:none和display:block添加到.dropdown内容也会破坏我想要的变换效果。
* {
font-family:sans-serif;
}
body {
background-image:linear-gradient(to right,#42b4ce,#fd3838);
}
.dropbtn {
background:none;
color: white;
font-size: 16px;
border: none;
cursor:pointer;
padding:0 0 10px;
}
.dropdown {
position: relative;
width: 160px;
margin: 0 auto;
display: block;
}
.dropdown-content {
opacity:0;
position: absolute;
background-color: #f1f1f1;
min-width: 400px;
right:-80px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: -1;
transform-origin: top center;
transform:rotate3d(-40, 4, 1.5, 45deg);
transition:.2s;
border-radius:10px;
}
.dropdown-content a {
color: black;
padding: 10px 14px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {color: purple;}
.dropdown:hover .dropdown-content {
opacity:1;
transform:rotate3d(0, 0, 0, 40deg);
z-index:0;
}
.dropdown:hover .dropbtn {text-shadow:1px 1px 2px rgba(0,0,0,0.6)};
<body>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
</body>
答案 0 :(得分:1)
将visibility: hidden;
添加到.dropdown-content
类中,并将visibility: visible
悬停在.dropdown
上。参见下面的代码。
* {
font-family:sans-serif;
}
body {
background-image:linear-gradient(to right,#42b4ce,#fd3838);
}
.dropbtn {
background:none;
color: white;
font-size: 16px;
border: none;
cursor:pointer;
padding:0 0 10px;
}
.dropdown {
position: relative;
width: 160px;
margin: 0 auto;
display: block;
}
.dropdown-content {
opacity:0;
position: absolute;
background-color: #f1f1f1;
min-width: 400px;
right:-80px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: -1;
transform-origin: top center;
transform:rotate3d(-40, 4, 1.5, 45deg);
transition:.2s;
border-radius:10px;
visibility: hidden;
}
.dropdown-content a {
color: black;
padding: 10px 14px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {color: purple;}
.dropdown:hover .dropdown-content {
opacity:1;
transform:rotate3d(0, 0, 0, 40deg);
z-index:0;
visibility: visible;
}
.dropdown:hover .dropbtn {text-shadow:1px 1px 2px rgba(0,0,0,0.6)};
<body>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
</body>
答案 1 :(得分:0)
只需为pointer-events: none;
添加.dropdown-content
,为选择器pointer-events: auto
添加.dropdown:hover .dropdown-content