简体   繁体   English

如何在移动设备上关闭基于悬停的CSS下拉菜单

[英]How to close hover based CSS drop down menu on mobile

I'm trying to make a web page that will dynamically work with desktops, tablets and mobile devices. 我正在尝试制作一个可与台式机,平板电脑和移动设备动态配合的网页。 I made a CSS only drop down menu based off of the hover selector (I think that's what it's called, I'm still very much a novice). 我基于悬停选择器制作了一个仅CSS的下拉菜单(我认为这就是所谓的,我仍然是一个新手)。 It works great on a desktop, it shows up fine on mobile, my problem is that the drop down menu wont hide itself on mobile when not being interacted with. 它在台式机上运行良好,在移动设备上显示良好,我的问题是,下拉菜单在不与手机交互时不会隐藏。 I understand that hover doesn't translate will to mobile devices, I was hoping that by tapping off of the drop down menu, it would hide itself again. 我了解到,悬停功能不会将意愿转换为移动设备,我希望通过点击下拉菜单来隐藏它。

I attempted to recreate the drop down menu with a button and a little javascript, but it became a mess to try to position the elements with css. 我试图用一个按钮和一些JavaScript重新创建下拉菜单,但是试图用CSS定位元素变得一团糟。 It seems the css only approach is the least complex to style. 似乎css only方法在样式上最简单。 I am open to ideas involving javascript though. 我对涉及javascript的想法持开放态度。

PS, please forgive the messy css, I haven't cleaned it up yet. PS,请原谅凌乱的CSS,我还没有清除它。 It's something I'm still learning to work with. 我仍在学习与之合作。

 <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;
}

The drop down menu will show up on a mobile if you tap the services list item. 如果您点击服务列表项,则下拉菜单将显示在移动设备上。 It just won't go away even after tapping off of it, only if you open a different link or refresh the page. 即使您打开它,即使您打开另一个链接或刷新页面,它也不会消失。 My hope was that tapping off of it would act the same as removing a courser from the hover point. 我的希望是,从中移走它的行为与从悬停点移除滑行器的行为相同。 It does not seem to be the case. 似乎并非如此。

Here is a technique to use a checkbox to provide a click view option on mobile. 这是一种使用复选框在移动设备上提供点击视图选项的技术。

NOTE: for the snippet to show the click working I have had to set the media query to a large resolution ( 1250 ) obviously you would put this lower in the live code. 注意:要使该片段显示单击效果,我不得不将media查询设置为较大的分辨率( 1250 ),显然您会将其放在实时代码中。

Some things to take note of. 一些事情要注意。

We now have a hidden checkbox, a label and spans with a for tag to activate the checkbox. 现在,我们有一个隐藏的复选框,一个标签以及一个带有for标记的跨度以激活该复选框。

Use a media query, fullscreen to hide the label . 全屏使用media查询来隐藏label

 nav { display: inline-block; background-color: blue; min-width: 50px; min-height: 25px; } nav .navCheck { display: none; } label[for="navCheck"] { display: none; } ul { display: none; color: #fff; } ul a, ul div a { text-decoration: none; color: white; } nav:hover ul { display: block; } @media screen and (max-width: 1250px) { label[for="navCheck"] { display: inline-block; width: 60px; height: 10px; } label[for="navCheck"] span { margin:2px; display: block; width: 50px; height: 2px; background-color: #fff; border: 1px solid #fff; border-radius: 7px; } input[class="navCheck"]:checked + ul { display: block; } } 
 <nav class="nav-main"> <label for="navCheck"> <span></span> <span></span> <span></span> </label> <input id="navCheck" type="checkbox" class="navCheck" /> <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> 

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

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