1

I want to create a dropdown menu (for mobile: media queries are already working) but I want to change the :hover effect of my menu button (CSS) to jquery .onclick() function.

I know my code isn't far away of working but I am not beeing able to fix what's wrong.

Anyone could give me a tip?

Heres the Fiddle: http://jsfiddle.net/4G3gg/

HTML

<ul class="navigation">
     <img src="img/menu.png" alt="mobile" width="50" height="50" id="mobile"/>
     <li class="n1"><a href="#">Home</a></li>
     <li class="n2"><a href="#">Portfolio</a></li>
     <li class="n3"><a href="#">Services</a></li>
     <li class="n4"><a href="#">About</a></li>
     <li class="n5"><a href="#">Contacts</a></li>
</ul>

CSS

#mobile:hover{background-color: rgba(255, 255, 255, 0.15);}

a{text-decoration: none;color: white; padding: 10px 50px; font-weight: bold}

a:hover { color: #777; font-weight: bold;}
/* NAVIGATION */
.navigation {
  list-style: none;
  padding: 0;
  width: 50px; 
  height: 40px; 
  margin: 0 auto;
  position: relative; 
  z-index: 100;
}

.navigation, .navigation a.main {
  border-radius: 4px;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
}

.navigation:hover, .navigation:hover a.main {
  border-radius: 4px 4px 0 0;
  -webkit-border-radius: 4px 4px 0 0;
  -moz-border-radius: 4px 4px 0 0;
}

.navigation a.main {
  display: block; 
  height: 30px;
  font: bold 15px/40px arial, sans-serif; 
  text-align: center; 
  text-decoration: none; 
  color: #FFF;  
  -webkit-transition: 0.2s ease-in-out;
  -o-transition: 0.2s ease-in-out;
  transition: 0.2s ease-in-out;
  position: relative;
  z-index: 100;
}

.navigation:hover a.main {
  color: rgba(255,255,255,0.6);
  background: rgba(0,0,0,0.04);
}

.navigation li { 
  width: 200px; 
  height: 40px;
  background: #131313;
  font: normal 12px/40px arial, sans-serif !important; 
  color: #999;
  text-align: center;
  margin: 0;
  -webkit-transform-origin: 50% 0%;
  -o-transform-origin: 50% 0%;
  transform-origin: 50% 0%;
  -webkit-transform: perspective(350px) rotateX(-90deg);
  -o-transform: perspective(350px) rotateX(-90deg);
  transform: perspective(350px) rotateX(-90deg);
  box-shadow: 0px 2px 10px rgba(0,0,0,0.05);
  -webkit-box-shadow: 0px 2px 10px rgba(0,0,0,0.05);
  -moz-box-shadow: 0px 2px 10px rgba(0,0,0,0.05);
}

.navigation li:nth-child(even) { background: #131313; }
.navigation li:nth-child(odd) { background: #1c1c1c; }

.navigation li.n1 { 
  -webkit-transition: 0.1s linear 0.1s;
  -o-transition: 0.1s linear 0.1s;
  transition: 0.1s linear 0.1s;
}
.navigation li.n2 {
  -webkit-transition: 0.1s linear 0.1s;
  -o-transition: 0.1s linear 0.1s;
  transition: 0.1s linear 0.1s;
}
.navigation li.n3 {
  -webkit-transition: 0.1s linear 0.1s;
  -o-transition: 0.1s linear 0.1s;
  transition: 0.1s linear 0.1s;
}
.navigation li.n4 { 
  -webkit-transition:0.1s linear 0.1s;
  -o-transition:0.1s linear 0.1s;
  transition:0.1s linear 0.1s;
}
.navigation li.n5 {
  border-radius: 0px 0px 4px 4px;
  -webkit-transition: 0.1s linear 0s;
  -o-transition: 0.1s linear 0s;
  transition: 0.1s linear 0s;
}

.navigation:hover li {
  -webkit-transform: perspective(350px) rotateX(0deg);
  -o-transform: perspective(350px) rotateX(0deg);
  transform: perspective(350px) rotateX(0deg);
  -webkit-transition:0.2s linear 0s;
  -o-transition:0.2s linear 0s;
  transition:0.2s linear 0s;
}
.navigation:hover .n2 {
  -webkit-transition-delay: 0.1s;
  -o-transition-delay: 0.1s;
  transition-delay: 0.1s;
}
.navigation:hover .n3 {
  -webkit-transition-delay: 0.15s;
  -o-transition-delay: 0.15s;
  transition-delay: 0.15s;
}
.navigation:hover .n4 {
  transition-delay: 0.2s;
  -o-transition-delay: 0.2s;
  transition-delay: 0.2s;
}
.navigation:hover .n5 {
  -webkit-transition-delay: 0.25s;
  -o-transition-delay: 0.25s;
  transition-delay: 0.25s;
}

JQUERY/JS

$(".navigation").addClass("js");
$(".navigation").addClass("js").before('<img src="img/menu.png" alt="mobile" width="50" height="50" id="mobile"/>');

$("#mobile").click(function(){
    $(".navigation").toggle();
});

1 Answer 1

3

Instead of the pseudo selector .navigation:hover use a new class like .navigation.open in css, then use .toggleClass() to toggle the menu visibility in the click() handler

jQuery(function ($) {
    //$(".navigation").addClass("js");
    //$(".navigation").addClass("js").before('<img src="img/menu.png" alt="mobile" width="50" height="50" id="mobile"/>');

    $("#mobile").click(function () {
        $(".navigation").toggleClass('open');
    });
})

Demo: Fiddle

Note: In your fiddle, you forgot to add jQuery library

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

1 Comment

Amazing, sorry for forgeting to add jquery to the Fiddle, i haven't tought of using a new class to open and close the menu.

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.