我想要一個元素在添加'push'類時變得固定,然後當類'push'被移除時變成相對的。toggle class if else css
這是我有:
$("#menuToggle").click(function() {
$('.wrapper,header,nav').toggleClass('push', function() {
if ($('.wrapper,header,nav').hasClass('push')) {
$('.wrapper').css('position', 'fixed');
} else {
$('.wrapper').css('position', 'relative');
}
});
});
這裏是CSS:
.push {
left:200px;
position:fixed;
}
.wrapper {
position:relative;
float:left;
top:0;
width:100%;
z-index:2;
background:#fff;
transition: all 1s ease;
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
}
nav {
position:fixed;
line-height:50px;
margin-left:-200px;
z-index:0;
width:200px;
height:100%;
float:left;
color:#fff;
background:#030e17;
}
header {
position:fixed;
z-index:10;
float:left;
width:100%;
background:#fff;
border-bottom:solid 1px #CCC;
transition: all 1s ease;
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
}
這是爲什麼不工作?
謝謝
爲什麼不加'.push {位置是:固定; }'到你的樣式表,或者只是針對你的包裝:'.wrapper.push {position:fixed; }'? – Pete 2014-09-30 15:03:54
不是沒有你的HTML – 2014-09-30 15:03:53
['toggleClass'](http://api.jquery.com/toggleclass/)沒有這樣的過載。 – Stijn 2014-09-30 15:06:51