我的頁面左側有一個div,右側有一個div。我希望這兩個動畫都是「隱藏」和「顯示」的位置,就像側面菜單一樣。我設法得到一個像這樣的工作:如何在jQuery中擁有多個點擊功能?
$(document).ready(function() {
$(".header-tab").click(function() {
if ($(".header").position().left == 0) {
$(".header").animate({left:'-150px'}, "slow");
$(".header-tab").animate({left:'0'}, "slow");
} else {
$(".header").animate({left:'0'}, "slow");
$(".header-tab").animate({left:'150px'}, "slow");
}
});
});
但我這樣做是爲了有左,右兩側的工作,只有左側的工作原理:
$(document).ready(function() {
$(".header-tab").click(function() {
if ($(".header").position().left == 0) {
$(".header").animate({left:'-150px'}, "slow");
$(".header-tab").animate({left:'0'}, "slow");
} else {
$(".header").animate({left:'0'}, "slow");
$(".header-tab").animate({left:'150px'}, "slow");
}
});
$(".aside-tab").click(function() {
if ($(".aside").position().right == 0) {
$(".aside").animate({right:'-150px'}, "slow");
$(".aside-tab").animate({right:'0'}, "slow");
} else {
$(".aside").animate({right:'0'}, "slow");
$(".aside-tab").animate({right:'150px'}, "slow");
}
});
});
所以,有人可以告訴我在哪裏或我如何添加第二個點擊功能(旁白)?
編輯:
下面是HTML:
<!doctype html>
<html>
<head>
<title>Website Title</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/main.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</head>
<body>
<div class="header"></div>
<div class="header-tab"></div>
<div class="container"></div>
<div class="aside"></div>
<div class="aside-tab"></div>
</body>
</html>
請顯示HTML。保存猜測:) –
我會建議使用布爾值來確定如果邊是打開或關閉,而不是檢查位置== 0 – koga73