我只需要在else if块内设置animate()的animateLeft值,并在else if块中设置其他所有值都应相同。脚本如下:

 var timeoutID = window.setTimeout(
        function(){
        if (imgs.index(imgElement) == 0) {
            imgElement.animate({
                width: "315px",
                height: "225px",
                marginTop: "-50px",
                marginLeft: "-150px",
            }, 1500 );
            imgElement.css("z-index","100");
            imgElement.css("position","absolute");
            imgElement.css("opacity","1");
            }
        else if (imgs.index(imgElement) == 1) {
                     //The same thing but marginLeft: "-200px"
            }
         }, 1500);

最佳答案

只需将您的对象放入var中即可:

var timeoutID = window.setTimeout(
    function(){
        var animateArgs = {
            width: "315px",
            height: "225px",
            marginTop: "-50px",
            marginLeft: "-150px",
        };
        if (imgs.index(imgElement) == 0) {
            imgElement.animate(animateArgs, 1500 );
            imgElement.css("z-index","100");
            imgElement.css("position","absolute");
            imgElement.css("opacity","1");
        }
        else if (imgs.index(imgElement) == 1) {
            animateArgs.marginLeft = "-200px";
            // etc etc etc
        }
    },
    1500
);

关于javascript - jQuery代码优化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6751750/

10-12 02:21