1

これは私のコードです

var bodyWidth = ($(document).width()/2);
console.log(bodyWidth);
$('.freeshipping').css('right',bodyWidth);

それは問題ありませんが、この関数の本文のサイズ変更とページの更新を行いたいと思います。どのように?

4

4 に答える 4

2
$(window).resize(function(){
    var bodyWidth = ($(document).width()/2);
    console.log(bodyWidth);
    $('.freeshipping').css('right',bodyWidth);
});

更新:関数を1回呼び出します:

$(window).resize((function(){
   var bodyWidth = ($(document).width()/2);
   console.log(bodyWidth);
   $('.freeshipping').css('right',bodyWidth);

   return arguments.callee;
})());​
于 2012-05-17T11:27:40.913 に答える
1
$(window).on('resize', function() {
   var bodyWidth = ($(document).width()/2);
   console.log(bodyWidth);
   $('.freeshipping').css('right',bodyWidth);
});

また、(明示的なサイズ変更アクションなしで)1回も実行する必要がある場合は、trigger()メソッドをチェーンします

$(window).on('resize', function() {
   var bodyWidth = ($(document).width()/2);
   console.log(bodyWidth);
   $('.freeshipping').css('right',bodyWidth);
}).trigger('resize');

DOM要素をターゲットにして$('.freeshipping')いるため、このスニペットはdomreadyイベントで必ずラップしてください。

于 2012-05-17T11:27:22.193 に答える
0

これを試して

 $(window).resize(function() {
     var bodyWidth = ($(document).width()/2);
    console.log(bodyWidth);
    $('.freeshipping').css('right',bodyWidth);
    });

詳細: http: //api.jquery.com/resize/

于 2012-05-17T11:27:11.273 に答える
0

さて、誰もが問題ない質問に答えましたが、!別のサンプルを次に示します。

    //store temporarily the existing method
    var resize = $.fn.resize;

    //overwrite $(window).resize with your method
    $(window).resize(function () {

        //here you do your stuff
        stuff.setWidth(param);

        //call the original
        resize;
    });

違いは、あなたが$(window).resize(function () { /* ... */ });失いたくない、ただ拡張するだけの方法をすでに持っているということです。私が自分のjQueryプラグインを書いていたときに、この問題に遭遇しました。ユーザー/開発者によって追加された既存の$(window).resize()メソッドを検討する必要がありました。したがって、この方法でそれを保持し、自分のプラグインを更新するために必要なメソッド/関数を実行します。

于 2012-05-17T11:35:30.987 に答える