0

jQuery.noConflict();を呼び出すコードを作成しました。何度も。jQuery.noConflict();を減らす方法 ステートメントが数回繰り返されるのは良くないと思うのでプレゼンス

$glob = jQuery.noConflict();

            /* functions to run when content loads */
            $glob(document).ready(function(){

                var ht = $glob(window).height();

                $glob('.content').css({
                    'height': ht,
                    'min-height': ht
                });

                $glob('#page').css({
                    'top':-ht
                });
            });


            /* functions to run when page loads */
            function animateHorizontal2id(id,msg){ //the id to animate, the easing type



                var $a = jQuery.noConflict(); // defined to remove jquery conflict error
                var animSpeed=2000; //set animation speed
                var page= $a("#page"); //define the page to move

                var ht = $a(window).height();



                //do the animation

                var archPos = $a(id).position().left;           

                $a('#architecture').css({
                    'left': archPos
                });



                page.stop().animate({"left": -($a(id).position().left)}, animSpeed

                                    );

                    $a('#page').css({
                        'top': -ht
                    });
                //}

            }
            function animateVertical2id(id,msg) {

                var $arct = jQuery.noConflict(); // defined to remove jquery conflict error
                var animSpeed=2000; //set animation speed
                var page= $arct("#page"); //define the page to move
                //do the animation

                page.stop().animate({"top": -($arct(id).position().top)}, animSpeed);

            }

            function animate2Home(id,msg) {
                var $home = jQuery.noConflict(); // defined to remove jquery conflict error

                var archPos = $home(id).position().left;            

                var ht = $home(window).height();
                $home('#page').css({
                    'top': -ht
                });

                $home('#architecture').css({
                    'left': archPos
                });


                var animSpeed=2000; //set animation speed
                var page= $home("#page"); //define the page to move
                //do the animation
                page.stop().animate({"left": -($home(id).position().left)}, animSpeed);
            }
4

3 に答える 3

3

次のような関数に含めることができます。

jQuery.noConflict();
(function($) {

   // You can use $ safely in here with no conflicts.

})(jQuery);

よりコンパクトにしたい場合は、この方法で行うこともできます。

jQuery.noConflict()(function(){
    // You can use $ safely in here with no conflicts.
}); 
于 2012-10-23T11:11:24.753 に答える
0

重要なの.noConflict()は、jQueryを別のグローバルに一度再割り当てしてから、必要な場所で新しいグローバルを参照することです。コードは次のように簡単に記述できます。

$glob = jQuery.noConflict();
/* functions to run when content loads */
$glob(document).ready(function () {
    var ht = $glob(window).height();
    $glob('.content').css({
        'height': ht,
        'min-height': ht
    });
    $glob('#page').css({
        'top': -ht
    });
});
/* functions to run when page loads */
function animateHorizontal2id(id, msg) { //the id to animate, the easing type
    var animSpeed = 2000; //set animation speed
    var page = $glob("#page"); //define the page to move
    var ht = $glob(window).height();
    //do the animation
    var archPos = $glob(id).position().left;
    $glob('#architecture').css({
        'left': archPos
    });
    page.stop().animate({
        "left": -($glob(id).position().left)
    }, animSpeed);
    $glob('#page').css({
        'top': -ht
    });
    //}
}

function animateVertical2id(id, msg) {
    var animSpeed = 2000; //set animation speed
    var page = $glob("#page"); //define the page to move
    //do the animation
    page.stop().animate({
        "top": -($glob(id).position().top)
    }, animSpeed);
}

function animate2Home(id, msg) {
    var archPos = $glob(id).position().left;
    var ht = $glob(window).height();
    $glob('#page').css({
        'top': -ht
    });
    $glob('#architecture').css({
        'left': archPos
    });
    var animSpeed = 2000; //set animation speed
    var page = $glob("#page"); //define the page to move
    //do the animation
    page.stop().animate({
        "left": -($glob(id).position().left)
    }, animSpeed);
}

そうは言っても、私はAllan Kimmer Jensenのソリューションが本当に好きですが、次のように書きます。

(function ($) {
    // You can use $ safely in here with no conflicts.
}(jQuery.noConflict()))
于 2012-10-23T11:15:55.270 に答える
0

ここで一度使用すれば完了です。

$(document).ready(function(){

  $.noConflict();

  //put your code here

});
于 2012-10-23T11:09:09.463 に答える