0

Trying to change my navigation code from hover to click as I've realized there's no "hover" on mobile. I tried searching if there's such thing as :click in CSS, but closest I've found is :target, which doesn't do the same thing as clicking. I'm just looking for advice for the most efficient way to change the code I have now to make it clickable.


.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>
3
  • 1
    Can you use JavaScript or CSS only? Commented Apr 22, 2019 at 21:24
  • 2
    With your current markup its not possible to use CSS only. You would need to add an input, then you could use input:checked as a selector. Otherwise use JS to add/remove a class to the UL. Commented Apr 22, 2019 at 21:27
  • I don't mind adding javascript code, but I'm not familiar with it. If it's something someone can write up quickly, i'd appreciate that. Commented Apr 22, 2019 at 21:33

4 Answers 4

2

Try this with using jQuery [change ".item.has-children:hover .submenu" with "submenuActive" and add js(jQuery) code]:

$(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>

Sign up to request clarification or add additional context in comments.

7 Comments

How do I make it that if they click on it a second time, it pops back up. I tried .click, but I think having two clicks cancel each other out. I tried .dblclick, that's what I'm using at the moment, but would rather one click. And do you know how to show that arrow key to notify it's a dropdown?
For arrow keys, you can use FontAwesome (fontawesome.com/icons/caret-down?style=solid). onClick you must change arrow key. For detecting second click, you can define a variable to count clicks: [step 1] define clickCount variable, [step 2] onclick: set clickCount=clickCount+1 (if clickCount%2==0 submenu addclass, else submenu remove class)
I tried my best to write the code, I've never written javascript, but it's sending me an error "unexpected token countClick" Here's what I put together lol: let countClick = 0 onclick = Set clickCount = clickCount + 1 if clickCount %2 == 0 { ('.submenu').addClass } else { ('.submenu').removeClass }
I wasn't sure if that should go before or after the other javascript
countClick !=clickCount :) I have updated my answer
|
0

If I understand correctly, CSS property "Active" is what you want to achieve? https://www.w3schools.com/cssref/sel_active.asp

3 Comments

No that seems to just style a button or item that's clicked. I need to turn a hover drop down to a clickable drop down
codepen.io/whiteart/pen/dwgyn how about this? Change to mobile mode and check if it works for you. Unfortunately, I can't offer solutions other than using JavaScript
I'm open to adding javascript. I'm just not familiar with javascript.
0

Maybe you are looking for pseudo-clase CSS :focus-within https://developer.mozilla.org/es/docs/Web/CSS/:focus-within

Comments

0

You can as well use JavaScript by:

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

then change the .item.has-children:hover .submenu to a different class. In my case I used newMenu. Populate the newMenu class with your desired style.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.