上个星期我被困在一个问题上。我在网上查看了很多次,我想我找到了很多次解决方案,但似乎都没有效果。我想要的是以下内容:,当我单击元素A时,我希望更改元素B的类,
我的密码:
Html:
<div class="header">
<a class="nav-toggle" href="#"><span></span></a>
<nav class="navigation_menu id="nav">
<ul class="navigation_ul">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li class="exclude_under_line"><a href="#">Portfolio<span class="drop_arrow">▼</span></a>
<ul>
<li><a href="#">Trimester 1</a></li>
<li><a href="#">Trimester 2</a></li>
<li><a href="#">Trimester 3</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
<li><a href="#">Forum</a></li>
</ul>
</nav>
</div>
CSS:
/** General Style **/
body {
background:f5f5f5;
margin: 0px;
padding: 0px;
font-family: 'roboto', sans-serif;
font-weight: 300;
}
/** Text Style **/
a {
text-decoration: none;
color: inherit;
}
/** Header Style **/
.header {
background: #607d8b;
margin: 0px;
padding: 0px;
padding-bottom: 100px;
color: white;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
/** Navigation Menu **/
/** Basic Style **/
.navigation_menu > a {
display: none; /* Removes Show Navigation and Hide Navigation */
}
.navigation_menu ul{
margin: 0px; /* Removes the auto create margin around the ul element */
padding: 0px;
}
.navigation_menu li {
position: relative; /* Makes Drop Down Menu Work and being showed */
list-style-type: none; /* Removes dots before Li elements */
font-size:18px; /* Makes font size 18 */
}
.navigation_menu > ul > li {
height: 30px; /* Determines The width height between drop down and menu */
float: left; /* Makes them float besides each other */
padding-left: 10px;
padding-right: 10px;
padding-top: 6px;
padding-bottom: 2px;
}
.navigation_menu > ul > li > ul {
padding: 0px; /* Removes the automatic created padding around the drop down ul */
width: 140%; /* Makes the text in drop down menu (whos bigger then parent) not go to a second line */
margin-top: -4px; /* Pushes the drop down are go down 8 pixels */
position: absolute; /*Makes the child ul not affect the parent li */
}
.navigation_menu li ul {
display: none; /* Hides Drop Down */
margin-left: -10px;
}
.navigation_menu li:hover ul {
display: block; /* Makes Drop Down Menu Work again */
background: white;
box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
}
/** Drop Arrow Style **/
.drop_arrow {
font-size:12px; /* Edits the size of the drop arrow */
margin-left:4px; /* Moves it a little bit away from the text next to it */
line-height:0%; /* Makes the icon don't affect the word next to it */
}
/* Hamburger Style */
.nav-toggle {
display:none;
}
.nav-toggle { cursor: pointer; padding: 10px 35px 16px 0px; }
.nav-toggle span, .nav-toggle span:before, .nav-toggle span:after {
cursor: pointer;
border-radius: 1px;
height: 3px;
width: 20px;
background: white;
position: absolute;
display: block;
content: '';
}
.nav-toggle span:before {
top: -7px;
}
.nav-toggle span:after {
bottom: -7px;
}
.nav-toggle span, .nav-toggle span:before, .nav-toggle span:after {
transition: all 500ms ease-in-out;
}
.nav-toggle.active span {
background-color: transparent;
}
.nav-toggle.active span:before, .nav-toggle.active span:after {
top: 0;
}
.nav-toggle.active span:before {
transform: rotate(45deg);
}
.nav-toggle.active span:after {
transform: rotate(-45deg);
}
@media only screen and ( max-width: 400px ) {
.nav-toggle {
display:block;
}
.navigation_menu {
display: none;
}
.navigation_menu_new {
display: block;
}
}
Javascript:
document.querySelector( ".nav-toggle" )
.addEventListener( "click", function() {
this.classList.toggle( "active" );
});
因此,我基本想要的是,当我单击“Nav”div时,我希望"navigation_menu“类(当屏幕大小小于400)从.navigation_menu更改为.navigation_menu_new。我想要这个,所以菜单将从无效到可见后,访问者点击汉堡包。
这是codepen:http://codepen.io/anon/pen/YXVjrv的钢笔
提前感谢!
发布于 2015-06-08 11:23:13
一个简单的解决办法是:
$(".nav-toggle").click(function(){
$(".navigation_menu").toggleClass("new");
});
发布于 2015-06-08 11:35:30
您可以从这样的toggleClass函数中受益:
jQuery(document).ready(function(){
jQuery(".Nav-Toggle").click(function(){
jQuery(".navigation_menu").toggleClass("navigation_menu_new");
});
});
根据您希望实现的额外功能,您可能希望探索使用addClass/removeClass,但是为了“交换”类的目的,toggleClass是您所需要的。一定要像我在一个较小的例子中所做的那样测试它,这样您就不必用大图( jsFiddle )使自己变得过于复杂。
https://stackoverflow.com/questions/30717325
复制相似问题