\r\n```\r\n\r\n上記のコードですと、先頭の.section-horizontal imgの横幅を元にした.section-horizontal imgのn個の合計が\r\n#contentsのwidth横幅になってしまいます。\r\n\r\n実現したいことは、.section-horizontal imgの横幅は画像サイズによって異なるため、各横幅サイズの合計を取得したいのですが、解決方法を教えていただけますしょうか。\r\n\r\n```ここに言語を入力\r\n
\r\n\r\n
\r\n\r\n\r\n\r\n
\r\n\r\n.\r\n.\r\n\r\n```\r\nアドバンスカスタムフィールドで画像をページ投稿表示しております。\r\n\r\nよろしくお願いします。","answerCount":1,"upvoteCount":0,"datePublished":"2016-01-10T21:15:44.190Z","dateModified":"2016-01-10T23:42:43.701Z","acceptedAnswer":{"@type":"Answer","text":"`jQuery('.section-horizontal img').outerWidth(true)`の部分を以下のように各画像幅の合計(`sum`)にしてはいかがですか。\r\n\r\n```\r\n// var contW = jQuery('.section-horizontal img').outerWidth(true) * jQuery('div a img', cont).length + 640;\r\nvar sum = 0;\r\nvar contImgs = jQuery('div a img', cont);\r\nfor(var i = 0; i < contImgs.length; i++) sum += contImgs.eq(i).outerWidth(true);\r\nvar contW = sum * contImgs.length + 640;\r\n```","dateModified":"2016-01-11T01:27:51.938Z","datePublished":"2016-01-11T01:27:51.938Z","upvoteCount":1,"url":"https://teratail.com/questions/24343#reply-38369"},"suggestedAnswer":[],"breadcrumb":{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://teratail.com","name":"トップ"}},{"@type":"ListItem","position":2,"item":{"@id":"https://teratail.com/tags/WordPress","name":"WordPressに関する質問"}},{"@type":"ListItem","position":3,"item":{"@id":"https://teratail.com/questions/24343","name":"【jQuery】 要素の幅サイズをすべて取得したい (n個のdiv imgの横幅が取得出来ない)"}}]}}}
<script type="text/javascript">
$(window).load(function(){
    var cont = jQuery('#contents');
    var contW = jQuery('.section-horizontal img').outerWidth(true) * jQuery('div a img',cont).length + 640 ; /*  640pxはtext分を足した数 */
    cont.css('width', contW);  
    var speed = 500;
    jQuery('html').mousewheel(function(event, mov) {
        //ie firefox
        jQuery(this).scrollLeft(jQuery(this).scrollLeft() - mov * speed);
        //webkit
        jQuery('body').scrollLeft(jQuery('body').scrollLeft() - mov * speed);
        return false;   //縦スクロール不可
    });
});
</script>
 
上記のコードですと、先頭の.section-horizontal imgの横幅を元にした.section-horizontal imgのn個の合計が 
#contentsのwidth横幅になってしまいます。
実現したいことは、.section-horizontal imgの横幅は画像サイズによって異なるため、各横幅サイズの合計を取得したいのですが、解決方法を教えていただけますしょうか。
<div id="contents">
<?php if( get_field('img01') ): ?>
<div id="s01" class="section-horizontal">
<a href="#s02"><img src="<?php the_field('img01'); ?>"></a>
</div>
<?php endif; ?>
<?php if( get_field('img02') ): ?>
<div id="s02" class="section-horizontal">
<a href="#s03"><img src="<?php the_field('img02'); ?>"></a>
</div>
<?php endif; ?>
.
.
 
アドバンスカスタムフィールドで画像をページ投稿表示しております。
よろしくお願いします。
 ベストアンサー
jQuery('.section-horizontal img').outerWidth(true)の部分を以下のように各画像幅の合計(sum)にしてはいかがですか。
// var contW = jQuery('.section-horizontal img').outerWidth(true) * jQuery('div a img', cont).length + 640;
var sum = 0;
var contImgs = jQuery('div a img', cont);
for(var i = 0; i < contImgs.length; i++) sum += contImgs.eq(i).outerWidth(true);
var contW = sum * contImgs.length + 640;
15分調べてもわからないことは teratailで質問しよう!
ただいまの回答率 85.29%
質問をまとめることで 思考を整理して素早く解決
テンプレート機能で 簡単に質問をまとめる
質問する 
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/01/11 01:51
退会済みユーザー
2016/01/11 02:07
退会済みユーザー
2016/12/17 01:48 編集
退会済みユーザー
2016/01/11 02:30
退会済みユーザー
2016/01/11 02:49