Tapas Mahata
Tapas Mahata

Reputation: 351

How to change menu from on hover to on click

I have one menu written using ul/li and used the below css to work on mouse-over. now I need to change this on-mouse over behavior to on-click. how to do? Please help.

#nav {
        float: left !important;
        font: bold 12px Century Gothic, Arial Rounded MT Bold, Arial, Helvetica, Sans-serif;
        border: 1px solid #121314;
        border-top: 1px solid #2b2e30;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 5px;
        
}



#nav ul {
        margin:0;
        padding:0;
        list-style:none;
}

#nav ul li {
        float:left !important;
}

#nav ul li a {
        float: left !important;
        color:deepskyblue;
        padding: 10px 20px;
        text-decoration:none;
        background:#3C4042;
        background: -webkit-gradient( linear, left bottom, left top, color-stop(0.09, rgb(59,63,65)), color-stop(0.55, rgb(72,76,77)), color-stop(0.78, rgb(75,77,77)) );
        background: -moz-linear-gradient( center bottom, rgb(59,63,65) 9%, rgb(72,76,77) 55%, rgb(75,77,77) 78% );
        background: -o-linear-gradient( center bottom, rgb(59,63,65) 9%, rgb(72,76,77) 55%, rgb(75,77,77) 78% );
        box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1) inset, 0 0 5px rgba(0, 0, 0, 0.1) inset;
        border-left: 1px solid rgba(255, 255, 255, 0.05);
        border-right: 1px solid rgba(0,0,0,0.2);
        text-shadow: 0 -1px 1px rgba(0, 0, 0, 0.6);
}

#nav li ul {
        background:#3C4042;
        background-image: -webkit-gradient( linear, left bottom, left top, color-stop(0.09, rgb(77,79,79)), color-stop(0.55, rgb(67,70,71)), color-stop(0.78, rgb(69,70,71)) );
        background-image: -moz-linear-gradient( center bottom, rgb(77,79,79) 9%, rgb(67,70,71) 55%, rgb(69,70,71) 78% );
        background-image: -o-linear-gradient( center bottom, rgb(77,79,79) 9%, rgb(67,70,71) 55%, rgb(69,70,71) 78% );
        border-radius: 0 0 10px 10px;
        -moz-border-radius: 0 0 10px 10px;
        -webkit-border-radius: 0 0 10px 10px;
        left: -999em;
        margin: 35px 0 0;
        position: absolute;
        width: 160px;
        z-index: 9999;
        box-shadow: 0 0 15px rgba(0, 0, 0, 0.4) inset; 
        -moz-box-shadow: 0 0 15px rgba(0, 0, 0, 0.4) inset; 
        -webkit-box-shadow: 0 0 15px rgba(0, 0, 0, 0.4) inset; 
        border: 1px solid rgba(0, 0, 0, 0.5);
}

#nav li ul a {
        background: none;
        border: 0 none;
        margin-right: 0;
        padding-top:3px;
        padding-bottom:3px;
        width: 120px;
        box-shadow: none;
        -moz-box-shadow: none;
        -webkit-box-shadow: none;
        border-bottom: 1px solid transparent;
        border-top: 1px solid transparent;
        text-align:left;
}

#nav ul li a:hover,
#nav ul li:hover > a {
        color: White;
        background:#5C9ACD;
        background: -webkit-gradient( linear, left bottom, left top, color-stop(0.09, rgb(77,79,79)), color-stop(0.55, rgb(67,70,71)), color-stop(0.78, rgb(69,70,71)) );
        background: -moz-linear-gradient( center bottom, rgb(77,79,79) 9%, rgb(67,70,71) 55%, rgb(69,70,71) 78% );
        background: -o-linear-gradient( center bottom, rgb(77,79,79) 9%, rgb(67,70,71) 55%, rgb(69,70,71) 78% );
        text-shadow: 0 1px 0 rgba(255, 255, 255, 0.2), 0 -1px #000;

}

#nav li ul a:hover, 
#nav ul li li:hover > a  {
    color: White;
        background: #5C9ACD;
        background: -webkit-gradient( linear, left bottom, left top, color-stop(0.17, rgb(61,111,177)), color-stop(0.51, rgb(80,136,199)), color-stop(1, rgb(92,154,205)) );
        background: -moz-linear-gradient( center bottom, rgb(61,111,177) 17%, rgb(80,136,199) 51%, rgb(92,154,205) 100% );
        background: -o-linear-gradient( center bottom, rgb(61,111,177) 17%, rgb(80,136,199) 51%, rgb(92,154,205) 100% );
        border-bottom: 1px solid rgba(0,0,0,0.6);
        border-top: 1px solid #7BAED9;
        text-shadow: 0 1px rgba(255, 255, 255, 0.3);
}



#nav li:hover ul {
        left:auto;
}


#nav li li ul {
        margin: -1px 0 0 160px;
        -webkit-border-radius: 0 10px 10px 10px;
        -moz-border-radius: 0 10px 10px 10px;
        border-radius: 0 10px 10px 10px;
        visibility:hidden;
}

#nav li li:hover ul {
        visibility:visible;
}

#nav ul ul li:last-child > a {
        -moz-border-radius:0 0 10px 10px;
        -webkit-border-radius:0 0 10px 10px;
        border-radius:0 0 10px 10px;
}

#nav ul ul ul li:first-child > a {
        -moz-border-radius:0 10px 0 0;
        -webkit-border-radius:0 10px 0 0;
        border-radius:0 10px 0 0;
}
<div id="nav">
  <ul>
    <li><a href="/Website/Home">Home</a></li>
    <li><a>Goal</a>
        <ul>
            <li><a href="../Items/Create">New</a></li>
            <li><a href="/Items/List">Search/View</a></li>
        </ul>
    </li>
    <li><a>Opportunity</a>
        <ul>
            <li><a href="/Requirement/Create">New</a></li>
            <li><a href="/Requirement/List">Search/View</a></li>
        </ul>
    </li>
    <li><a>Alerts</a></li>
    <li><a>Reports</a></li>
    <li><a>Administration</a></li>
</ul>
  </div>

I am note sure but will this work on-click if the :hover is changed to :active and the same is applied from jquery. Please advise.

Upvotes: 1

Views: 122

Answers (4)

lshettyl
lshettyl

Reputation: 8181

First off, your CSS could be improved a lot. To achieve what you were after:

Add a class to the drop-down menus, such as the following.

<ul class="dropdown">
    <li><a href="/Requirement/Create">New</a></li>
    <li><a href="/Requirement/List">Search/View</a></li>
</ul>

Remove left: -999em; from #nav li ul {..} class.

Add the following CSS.

#nav .dropdown { display: none; }
#nav .active .dropdown { display: block; }

And the following jQuery code.

$(function() {
    var $nav = $("#nav");
    var $items = $nav.find(" > ul > li");

    $items.on("click", function(evt) {
        evt.preventDefault();        
        var $current = $(this);
        var $toggle = $current.is(".active") ? 'removeClass' : 'addClass';
        $current.siblings().removeClass("active").end()[$toggle]("active");
    });
});

Here is a demo I've created putting all the above together. Hope that helps.

Upvotes: 1

Shaggy
Shaggy

Reputation: 6796

You can achieve this through CSS alone, without the need for any JavaScript by giving the parent <li>s a tabindex attribute and then using the :focus pseudo class instead of :hover.

The one thing you'll sacrifice with this approach, though, is the ability to close the submenu by clicking on the parent element a second time.

Here's a very quick example.

*{box-sizing:border-box;color:#fff;font-family:arial;font-size:14px;margin:0;padding:0;}
ul{
    background:#000;
    line-height:30px;
    list-style:none;
}
#menu>li{
    position:relative;
}
p{
    cursor:pointer;
    padding:0 5px;
}
ul ul{
    border-top:1px solid #fff;
    position:absolute;
    display:none;
    left:0;
    text-indent:5px;
    top:30px;
    min-width:100%;
}
#menu>li:focus ul{
    display:block;
}
<ul id="menu">
    <li tabindex="-1">
        <p>Menu</p>
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
        </ul>
    </li>
</ul>

Upvotes: 0

SM Ahmed
SM Ahmed

Reputation: 128

Use Jquery for click events. Create a CSS class with the hover properties and add that class to to your element

eg:-

$('elementToBeClickedUpon').click(function(){
  $('elementToBeClickedUpon').addClass('classToBeAdded')
})

Upvotes: 0

bytesized
bytesized

Reputation: 1522

If you are trying to use pure CSS, you could use :target

<a href="#some_id">Show things on click</a>

When this is clicked, css like

#some_id:target {...}

will activate.

Upvotes: 1

Related Questions