将悬停下拉菜单转换为可点击的下拉菜单

时间:2019-04-22 21:14:06

标签: html css

尝试将导航代码从悬停更改为单击,因为我意识到移动设备上没有“悬停”。我尝试搜索CSS中是否存在:click,但是我发现最接近的是:target,它与click的作用不同。 我只是在寻找有关更改代码的最有效方法的建议,以使其可单击。


.nav {
    position: sticky;
    left: 0;
    background-color: rgba(255,255,255,.8);
    border-radius: 0px;
    border: none;
    width: 100%;
    padding: 10px 0;
    flex-direction: row;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
}

.item {
    color: black;
    font-weight: bold;
    text-transform: capitalize;
    width: 25%;
    text-align: center;
}

.submenu {
    display: none;
    flex-wrap: wrap;
    align-items: center;
    align-text: center;
    position: absolute;
    padding-top: 107px;
    padding: 10px;
    font-size: small;
    left: 0;
    right: 0;
    text-transform: capitalize;
    z-index: 1;
    background-color: #2F4F4F;
    color: white;
    justify-content: left;
}

.submenu li {
    margin-left: 6%;
    width: 19%;
    padding: 5px;
}

.item.has-children:hover .submenu {
    display: flex;
    align-items: center;
    flex-direction: row;
    justify-content: left;
    flex-wrap: wrap;
    flex: 1 1 calc(25% - 80px);
    color: black;
    background-color: rgba(255,255,255,.8);
}
    <ul class="nav">
        <li class="item">
            <a href="index.html">
                <img src="../Images/Navigation/Intak Nav Mark -01.png" alt="Home"/>
            </a>
        </li>
        <li class="item has-children"><a href="Printing.html">Printing</a>

        </li>
        <li class="item has-children" style="color:#4D4D4D;">Graphic Design

        </li>
        <li class="item has-children">Chinese Calendars
            <ul class="submenu">
                <li><a href="Calendars/Cane Wallscroll Calendars.html">Cane Wallscroll Calendars</a></li>
                <li><a href="Calendars/Wall Calendars.html">Wall Calendars</a></li>
                <li><a href="Calendars/Mini Calendars.html">Mini Calendars</a></li>
                <li><a href="Calendars/Desk Calendars.html">Desk Calendars</a></li>
                <li><a href="Calendars/Special Desk Calendars.html">Special Desk Calendars</a></li>
                <li><a href="Calendars/Lucky Money Envelopes.html">Lucky Money Envelopes</a></li>
                <li><a href="Calendars/More.html">More Calendars</a></li>
            </ul>
        </li>
        <li class="item"><a href="Contact Us.html">Contact Us</a></li>
    </ul>

4 个答案:

答案 0 :(得分:2)

尝试使用jQuery [将“ .item.has-children:hover .submenu”更改为“ submenuActive”,并添加js(jQuery)代码]:

$(document).ready(function() {
  var countClick = 0; 
  $( "ul.nav li.has-children" ).click(function() {
    countClick = countClick + 1;
    if (countClick %2 == 0) {
      $(this).children('.submenu').addClass('submenuActive');
    } 
    else 
    { 
      $(this).children('.submenu').removeClass('submenuActive');
    } 
  });
  $( "ul.nav li.has-children" ).mouseleave(function() {
    countClick = 0;
    $(this).children('.submenu').removeClass('submenuActive');
  });
  
});
.nav {
    position: sticky;
    left: 0;
    background-color: rgba(255,255,255,.8);
    border-radius: 0px;
    border: none;
    width: 100%;
    padding: 10px 0;
    flex-direction: row;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
}

.item {
    color: black;
    font-weight: bold;
    text-transform: capitalize;
    width: 25%;
    text-align: center;
}

.submenu {
    display: none;
    flex-wrap: wrap;
    align-items: center;
    align-text: center;
    position: absolute;
    padding-top: 107px;
    padding: 10px;
    font-size: small;
    left: 0;
    right: 0;
    text-transform: capitalize;
    z-index: 1;
    background-color: #2F4F4F;
    color: white;
    justify-content: left;
}

.submenu li {
    margin-left: 6%;
    width: 19%;
    padding: 5px;
}

.submenuActive {
    display: flex;
    align-items: center;
    flex-direction: row; 
    justify-content: left;
    flex-wrap: wrap;
    flex: 1 1 calc(25% - 80px);
    color: black;
    background-color: rgba(255,255,255,.8);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="nav">
        <li class="item"> 
            <a href="index.html">
                <img src="../Images/Navigation/Intak Nav Mark -01.png" alt="Home"/>
            </a>
        </li>
        <li class="item has-children"><a href="Printing.html">Printing</a>

        </li>
        <li class="item has-children" style="color:#4D4D4D;">Graphic Design

        </li>
        <li class="item has-children">Chinese Calendars
            <ul class="submenu">
                <li><a href="Calendars/Cane Wallscroll Calendars.html">Cane Wallscroll Calendars</a></li>
                <li><a href="Calendars/Wall Calendars.html">Wall Calendars</a></li>
                <li><a href="Calendars/Mini Calendars.html">Mini Calendars</a></li>
                <li><a href="Calendars/Desk Calendars.html">Desk Calendars</a></li>
                <li><a href="Calendars/Special Desk Calendars.html">Special Desk Calendars</a></li>
                <li><a href="Calendars/Lucky Money Envelopes.html">Lucky Money Envelopes</a></li>
                <li><a href="Calendars/More.html">More Calendars</a></li>
            </ul>
        </li>
        <li class="item"><a href="Contact Us.html">Contact Us</a></li>
    </ul>

答案 1 :(得分:0)

如果我理解正确,那么您想实现CSS属性“ Active”吗? https://www.w3schools.com/cssref/sel_active.asp

答案 2 :(得分:0)

也许您正在寻找伪语法CSS :focus-within https://developer.mozilla.org/es/docs/Web/CSS/:focus-within

答案 3 :(得分:0)

您还可以通过以下方式使用JavaScript:

`var all = document.querySelectorAll(".item.has-children");
all.forEach(function (e) {
    addEventListener('click', function (){
        e.classList.remove('submenu');
        e.classList.add('newMenu');
    })
})`

然后将.item.has-children:hover .submenu更改为其他类。就我而言,我使用了newMenu。用所需的样式填充newMenu类。