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 回答