【发布时间】:2019-04-17 13:12:10
【问题描述】:
我正在尝试制作一个可以在台式机、平板电脑和移动设备上动态运行的网页。我根据悬停选择器制作了一个仅 CSS 的下拉菜单(我认为这就是所谓的,我还是个新手)。它在桌面上运行良好,在移动设备上显示良好,我的问题是下拉菜单在不与移动设备交互时不会隐藏在移动设备上。我知道悬停不会将意志转化为移动设备,我希望通过点击下拉菜单,它会再次隐藏自己。
我尝试使用一个按钮和一些 JavaScript 重新创建下拉菜单,但尝试使用 css 定位元素变得一团糟。似乎只有 css 的方法是最不复杂的样式。不过,我对涉及 javascript 的想法持开放态度。
PS,请原谅乱七八糟的css,我还没有清理它。这是我仍在学习使用的东西。
<nav class="nav-main">
<ul>
<a href="index.html"><li>Home</li></a>
<a href="index.html#main"><li>About</li></a>
<li class="dropdown">
<a href="javascript:void(0)" class="dropbtn">Services</a>
<div id="menu-box" class="dropdown-content">
<a href="index.html#main">Overview</a>
<a href="index.html#main">General Practice</a>
<a href="index.html#main">Sports Physicals</a>
<a href="index.html#main">Weight Loss</a>
</div>
</li>
<a href="index.html#main"><li>Doctor's Daily Dose</li></a>
<a href="index.html#main"><li>Contact</li></a>
</ul>
</nav>
}
nav ul li:hover{
background-color: #D7868C;
border-radius: 0.3em;
}
@media only screen and (max-width: 874px){
nav ul {
display: flex;
flex-direction: column;
width: 100%;
}
nav ul li{
text-align: center;
flex-direction: column;
}
header {
flex-direction: column;
width: 100%
}
nav ul a{
display: flex;
flex-direction: column;
width: 100%;
}
nav{
flex-direction: column;
}
#logo {
display: block;
margin-left: auto;
margin-right: auto;
margin-bottom: 0.2em;
}
}
footer{
display: flex;
flex-wrap: wrap;
justify-content: space-between;
border-top: 1px solid #343434;
padding-top: 1em;
margin-left: 0.1em;
margin-right: 0.1em;
}
@media only screen and (max-width: 439px) {
.footSec{
display: inline-block;
width: 100%;
margin-left: auto;
margin-right: auto;
text-align: center;
}
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #D7868C;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #CEDDE6;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
如果您点击服务列表项,下拉菜单将显示在移动设备上。即使在点击它之后它也不会消失,只有当您打开不同的链接或刷新页面时。我的希望是,轻敲它与从悬停点移除骏马一样。好像不是这样的。
【问题讨论】:
标签: javascript html css