少し遅くなっているので、ばかげた間違いをしているのなら失礼します。何らかの理由で次のコード:
$(".myClass").each(function(){
widths[$(this).attr("id")] = $(this).width();
if ($(this).attr("id") != $(clickedExpand).attr("id"))
{
$(this).animate({
width: '10px'
});
}
});
配列は次のように初期化されます
var widths = new Array();
コードの前半。何らかの理由で、アニメーションが始まる前に幅を記録しているにもかかわらず、配列にアニメーション後の値が表示されています。アニメーションが終了し、値が記録されているように見えます。関数から取り出して別の.eachでラップしようとしましたが、同じ結果が得られています。
どんな助けでも大歓迎です!
コード全体:
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")]
});
});
});
});