如果我犯了一個愚蠢的錯誤,它遲了一點,所以請原諒。出於某種原因,下面的代碼:在動畫記錄發佈動畫值之前存儲寬度值
$(".myClass").each(function(){
widths[$(this).attr("id")] = $(this).width();
if ($(this).attr("id") != $(clickedExpand).attr("id"))
{
$(this).animate({
width: '10px'
});
}
});
該陣列之前在代碼初始化爲
var widths = new Array();
。出於某種原因,儘管我在動畫開始之前記錄了寬度,但我正在獲取數組中的動畫後值。看起來好像動畫完成,然後值被記錄下來。我試圖把它從函數中取出幷包裝在另一個函數中,但是我得到了相同的結果。
任何幫助將不勝感激!
整個代碼:
var slateWidths = {};
$(".slateExpand").click(function(){
var clickedExpand = $(this).closest(".slate");
$(".slate").each(function(){
slateWidths[$(this).attr("id")] = $(this).outerWidth();
if ($(this).attr("id") != $(clickedExpand).attr("id"))
{
$(this).animate({
width: '10px'
});
$(this).find($('.slateExpand')).hide();
}
});
$(this).text("Restore");
$(this).removeClass("slateExpand").addClass("slateRestore");
$(".slateRestore").on("click",function(){
$(".slate").each(function()
{
alert(slateWidths[$(this).attr("id")]);
//var width = slateWidths[$(this).attr("id")];
$(this).animate({
width: slateWidths[$(this).attr("id")]
});
});
});
});
切勿使用'new Array()',並且在實際需要對象時不要使用數組。 ('var widths = {};') – Ryan
有趣...怎麼回事?此外,這個問題仍然存在:( –
)你什麼時候得到錯誤的值?直接從'width()'方法,或者你設置/讓他們到別的地方?告訴我們整個代碼 – Bergi