1 回答
TA贡献1829条经验 获得超7个赞
if
你的“错误”——你在语句中放入了Setter .css( propertyName, value )
(Set the value too...),而不是Getter .css( propertyName )
(Get the value of...)。
if($("#drawer").css('margin-top') == "-5px"){ console.log("Do something"); }
基本片段:
$(document).ready(function(){
$("#drawer-drop").click(function(){
console.log($("#drawer").css('margin-top'));
if($("#drawer").css('margin-top') == "-5px"){
/* Setter */
$('#drawer').css('margin-top','0px');
$('#drawer').css('background','red');
}else{
/* Setter */
$('#drawer').css('margin-top','-5px');
$('#drawer').css('background','blue');
}
});
});
#drawer{
margin-top: 5px;
background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav id="drawer">
<h1>This will be a sweet menu.</h1>
</nav>
<a id="drawer-drop" href="#">MENU</a>
添加回答
举报