将鼠标悬停在任何中间导航项上时,都会出现一个水平下拉列表。我希望下拉菜单背景可以拉伸到全角,并使下拉菜单元素位于您悬停位置的中心。
我尝试将.dropdown:hover .dropdown-content的显示更改为flex,但是dropdown元素在屏幕上延伸,因此很难将鼠标悬停在屏幕上。
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
.site-logo {
margin-top: 2em;
}
.site-footer .email-newsletter .email-submit-btn {
text-transform: uppercase;
}
.site-header .primary-navigation .navigation-item-group {
text-transform: uppercase;
font-weight: 600;
font-size: 14px;
color: #444444;
}
.utility-navigation .nav-item.cart-nav-item {
background-color: #333333;
}
@media screen and (min-width: 769px) {
.topnav {
overflow: hidden;
margin-top: 3em;
padding-top: 0.5em;
display: inline;
float: left;
margin-right: 3.5%;
margin-left: 0;
margin-right: 0;
float: right;
text-align: right;
clear: right;
margin-top: 3em;
padding-top: 1em;
padding-bottom: 0.5em;
}
.topnav a {
float: left;
padding-bottom: 2em;
}
.dropdown:hover .dropdown-content {
visibility: visible;
opacity: 1;
transition: all 0.3s linear;
}
}
@media screen and (max-width: 769px) {
.topnav {
position: relative;
padding-bottom: 1em;
padding-top: 1em;
}
.topnav .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav a {
float: none;
display: block;
text-align: center;
}
.topnav .dropdown {float: none;}
.topnav .dropdown-content {
position: relative;
float:none;
text-align:center;
}
.topnav .dropdown .dropbtn {
display: block;
width: 100%;
text-align: center;
}
.dropdown-content a {
float: none !important;
text-align: center !important;
}
.dropdown:hover .dropdown-content {
display: block;
}
}
.topnav a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
text-transform: uppercase;
font-weight: 600;
font-size: 14px;
color: #444444;
}
.topnav .icon {
display: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
border: none;
outline: none;
padding: 14px 20px;
background-color: white;
font-family: inherit;
margin: 0;
text-transform: uppercase;
font-weight: 600;
font-size: 14px;
color: #444444;
}
.dropdown-content {
display: flex;
visibility: hidden;
opacity: 0;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
background-color: white;
position: absolute;
top: auto;
right: 0;
}
.dropdown-content a {
float: right;
padding: 20px 23px;
text-decoration: none;
display: block;
text-align: left;
}
body.home .site-header .container {
padding-bottom: 1em;
}
img.nav-img {
display: block;
margin-left: auto;
margin-right: auto;
}
<div class="topnav" id="myTopnav">
<a href="#" class="active"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">Calendar</a>
<div class="dropdown">
<button class="dropbtn"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">About
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Music</a>
<a href="#">More</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">Tickets
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Subscriptions</a>
<a href="#">Music</a>
<a href="#">Tickets</a>
<a href="#">More</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">Community
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Learning Programs</a>
<a href="#">More</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn"> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">Visit
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Seating Charts</a>
<a href="#">More</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">Give
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Donate Now!</a>
<a href="#">More</a>
</div>
</div>
<a href="#" class="active"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/220px-SNice.svg.png" height="22" class="nav-img">Shop</a>
<a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">☰</a>
</div>
我希望当用户将鼠标悬停在导航上并且背景变为全角时会生成下拉菜单。同样,下拉菜单元素应位于悬停位置的中心。它应该类似于laphil.org导航。
答案 0 :(得分:0)
几件事:
您将想要将.dropdown-content
更改为:
.dropdown-content {
display: flex;
visibility: hidden;
opacity: 0;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
background-color: white;
position: absolute;
top: auto;
width: 100vw;
left: 0;
text-align: center;
}
在.dropdown-content
中,注意添加了width
和text-align
属性,并删除了right
属性。
您还需要创建一个新的div,并在其中包裹下拉链接,如下所示:
<div class="dropdown-content">
<div class="sample">
<a href="#">Subscriptions</a>
<a href="#">Music</a>
<a href="#">Tickets</a>
<a href="#">More</a>
</div>
</div>
在这种情况下,新的sample
div具有以下样式:
.sample {
margin: 0 auto;
}
这是一个有效的代码笔:https://codepen.io/raymondwclark/pen/Bgvdwy