2011-02-10 29 views
1

すべての画像の合計幅と最後の画像の幅をコンテナdivにする必要があります。私はこれを使って結合画像の幅を得ました:すべての要素の幅+最後の要素の幅

$(window).load(function() { 
    $('.pressimage').each(function() { 
     var totalImageWidth = 0; 

     $("img", this).each(function() { 
      totalImageWidth += $(this).width(); 
     }); 

     $(this).width(totalImageWidth); 
    }); 
}); 

最後の画像の幅をどのように追加できますか?アイブ氏はこれを試してみましたが、それは動作しません:

$(window).load(function() { 
    $('.pressimage').each(function() { 
     var totalImageWidth = 0; 

     $("img", this).each(function() { 
      totalImageWidth += $(this).width(); 
     }); 


     $("img", this).last(function() { 
      lastImageWidth = $(this).width(); 
     }); 

     $(this).width(totalImageWidth + lastImageWidth); 
    }); 
}); 

おかげ

UPDATE。 申し訳ありませんが、間違ったコードを作成していました。

$(window).load(function() { 

    var pub_accum_width = 0; 

    $('.view-public-collection .pubimagecolor').find('img').each(function() { 
     pub_accum_width += $(this).width(); 
    }); 
    //alert(pub_accum_width); 

     $('.view-public-collection .view-content').width(pub_accum_width); 

}); 

そしてここでは、私の修正コードがあるが、それはdivの任意の幅広に形成されていません:

$(window).load(function() { 


    var pub_accum_width = 0; 
    var pub_accum_last = 0; 

    $('.view-public-collection .pubimagecolor').find('img').each(function() { 
      pub_accum_width += $(this).width(); 
    }); 


    $('.view-public-collection .pubimagecolor').find('img').last(function() { 
     pub_last_width += $(this).width(); 
    }); 


    $('.view-public-collection .view-content').width(pub_accum_width + pub_last_width); 


}); 

おかげ

ここで私は、そのすべての画像のdivの幅作るているものです

答えて

3

更新質問に対する回答:

$(window).load(function() { 
    var pub_accum_width = 0; 
    var pub_last_width = 0; // not pub_accum_last 

    $('.view-public-collection .pubimagecolor').find('img').each(function() { 
     pub_accum_width += $(this).width(); 
    }); 

    //             ...last(function(... 
    $('.view-public-collection .pubimagecolor').find('img').last().each(function() { 
     pub_last_width += $(this).width(); 
    }); 

    $('.view-public-collection .view-content').width(pub_accum_width + pub_last_width); 
}); 

last()は別のトラバース関数です。コールバックパラメータを追加することはできません。最後の要素を選択し、each()を使用します。また、異なる変数名を使用します。


あなたが外で再びそれを使用できるように、last() last()コールバック each()コールバックのlastImageWidth変数外を定義する必要があります。

// ... 
var totalImageWidth = 0; 
var lastImageWidth = 0; 
// ... 
+0

申し訳ありませんが、私は間違ったコードを投稿しました。これらのコメントに実際にコードを投稿できないので、私のメインポストを更新しました。ありがとうございました – Evans

+0

Working great。ありがとう – Evans

+0

私の最初のコードセクションでも、同じ方法でlast()を使う必要があります。 – Evans

関連する問題