简体   繁体   English

将css:悬停效果更改为jquery.onclick()

[英]Change css:hover effect to jquery.onclick()

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. 我想创建一个下拉菜单(对于移动设备:媒体查询已经在工作)但我想将菜单按钮(CSS)的:hover效果更改为jquery .onclick()函数。

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/ 继承人小提琴: http //jsfiddle.net/4G3gg/

HTML 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 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 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();
});

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 而不是伪选择器.navigation:hover在css中使用像.navigation.open这样的新类,然后使用.toggleClass()click()处理程序中切换菜单可见性

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 注意:在你的小提琴中,你忘了添加jQuery库

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

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