onload と onresize を組み合わせる (jQuery) 質問する

onload と onresize を組み合わせる (jQuery) 質問する

サイズ変更時だけでなくロード時にも関数を呼び出したいです。

これをもっとコンパクトに書き直す良い方法はありますか?

$('.content .right').width($(window).width() - (480));
$(window).resize(function(e) {
    $('.content .right').width($(window).width() - (480));
});

ベストアンサー1

resizeイベントのみにバインドし、ロード時にこのイベントを自動的にトリガーすることもできます。

// Bind to the resize event of the window object
$(window).on("resize", function () {
    // Set .right's width to the window width minus 480 pixels
    $(".content .right").width( $(this).width() - 480 );
// Invoke the resize event immediately
}).resize();

最後の.resize()呼び出しでは、ロード時にこのコードが実行されます。

おすすめ記事