繁体   English   中英

jQuery动画图像,位置,宽度和高度

[英]Jquery animating image, position, width and height

我有一种动画“ img”的情况,当我将var完整的内部click函数放置时,它不会进行动画处理,但是当我将其置于外部函数中时,它将起作用。它是否具有全局和局部作用域? 谢谢

var complete = true;


$("img").click(function() {

    if (complete) {

        $(this).animate({
            left: "50%",
            top: "400px",
            width: "300px",
            height: "300px"

        }, 700);

    } else {

        $(this).animate({
            left: "8px",
            top: "10px",
            width: "150px",
            height: "150px"
        }, 700);
    }

    complete = !complete;
});

如果你把

var complete = true;

click处理程序的回调内部,每次调用该函数时,它将被定义为true ,这实际上将该函数重新定义为

$("img").click(function() {

  if (true) {

    $(this).animate({

      left: "50%",
      top: "400px",
      width: "300px",
      height: "300px"

    }, 700);

  } else {

    $(this).animate({

      left: "8px",
      top: "10px",
      width: "150px",
      height: "150px"

    }, 700);

  }

});

并通过进一步简化:

$("img").click(function() {

  $(this).animate({

    left: "50%",
    top: "400px",
    width: "300px",
    height: "300px"

  }, 700);

});

通过在函数外部定义complete的变量,变量的值将保留到函数的单个调用之外,从而允许在每次调用时切换全局状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM