0
我試圖編寫一些jQuery代碼,獲取圖像在顯示之前被克隆和調整大小的組合寬度,問題在於它們之前被添加到DOM的寬度始終爲0使用jQuery克隆並調整大小後獲取圖像的大小
(function($){
var $images = createDiv(),
totalWidth = 0;
//loop through a list of images
$("#images > li > img").each(function(){
var $this = $(this),
$img = $this.clone(); //createa duplicate of the images
$img.height(100); //resize each duplicate to a set height
totalWidth += $img.width(); //always equals zero
//this always results in 0 too
//totalWidth += $img.get(0).clientWidth;
//add the cloned image to the div
$images.append($img);
//remove full sized image to reattach later
$this.remove();
});
$images.hide();
$("body").append($images);
$("#show").on('click',function(){
$images.show();
});
function createDiv(){
return $('<div id="imgs"></div>');
}
}(jQuery));
下面是一個完整的例子的小提琴:http://jsfiddle.net/bittersweetryan/9c3SH/2/
我怎樣才能得到縮放後的圖像的總組合寬度他們被添加到前DOM?