你的代碼是非常接近你所需要的。我認爲主要問題是你的風格和理解他們如何與效果互動。
摺疊效果將根據其當前狀態顯示或隱藏元素。你有類,它啓動你的div在0x0像素和隱藏,但你真正想要的是一個規則,描述你的div顯示給你的用戶時看起來像什麼。
原文:
.fold {
display: none;
width: 0;
height: 0;
position: absolute;
}
更正:
.fold {
width: 500px;
height: 500px;
position: absolute;
}
由於您的風格現在描述你的div的最終狀態,它不再具有display: none;
規則。既然你要的div最初被隱藏,你應該添加一些JavaScript來隱藏這些最初:
$(".fold").hide();
現在,而不是手工動畫的風格,你可以使用重功效:
原文:
// if there is visible fold element on page (user already clicked at least once on link)
if (active.length) {
active.animate({
width: "0"
}, 200)
.animate({
height: "0"
}, 200, function() {
// this happens after above animations are complete
$(this).removeClass("active");
})
// clicking for the first time
}
if (active.attr("id") != page) {
$("#" + page)
.addClass("active")
.animate({
height: "500px"
}, 1000, 'linear')
.animate({
width: "500px"
}, 400, 'linear')
}
更新時間:
if (active.length) {
active.toggle("fold", function() {
$(this).removeClass("active");
});
}
// clicking for the first time
if (active.attr("id") != page) {
$("#" + page)
.addClass("active")
.toggle("fold");
}
現在,所有這一切都worki恩,我想你會想玩一些設置。 documentation for the fold effect顯示您可以設置元素摺疊時的大小以及摺疊順序。爲了模仿你發佈的鏈接,我會將size
設置爲5,因爲你的div有5px的邊框。我也會設置horizFirst
到true
,因爲這就是你的例子所顯示的。
我也決定用toggleClass
代替addClass("active")
和removeClass("active")
。這導致更簡單的設置。
這裏的成品:
$(".fold").hide();
var foldSettings = {
easing: 'linear',
duration: 1200,
size: 5,
horizFirst: true,
complete: function() {
$(this).toggleClass("active");
}
}
$("a").click(function() {
var page = $(this).data("page");
if ($('div:animated').id != page) {
var active = $(".fold.active");
// if there is visible fold element on page (user already clicked at least once on link)
if (active.length) {
active.toggle("fold", foldSettings);
}
// clicking for the first time
if (active.attr("id") != page) {
$("#" + page).toggle("fold", foldSettings);
}
}
});
http://jsfiddle.net/z69zofxm/3/
檢查這個http://jsfiddle.net/6jv52yea/ – 2014-12-16 15:36:18