javascript - 单击更改元素位置

标签 javascript jquery css

我在 jQuery Mobile 中有这段代码,当我单击图像时它会变大。

我需要图像向左和向上移动。

我试试这个:

<div class="ui-block-b">
        <p><img  id="zPIC" height="150" width="150" onclick="ZX();"/></p>
</div>


function ZX() {
    var img = $("#zPIC");
    if (img.width() < 200) {
       img.animate({ width: "200px", height: "200px", left: "20px", top: "20px" }, 1000);
    }
    else {
       img.animate({ width: img.attr("width"), height: img.attr("height") }, 1000);
    }
}

但是图片只增长不横向移动

最佳答案

您需要设置位置属性,例如相对或绝对。例如:

<div id="zPIC" style="position:relative; width:150px; min-height:150px; background:green"></div>

$(document).ready(function(){
  $("#zPIC").click(function(){
    if ($("#zPIC").width() < 200) {
      // store the original sizes
      $(this).data('width', $(this).css('width'));
      $(this).data('height', $(this).css('height'));
      $("#zPIC").animate({ width: "200px", height: "200px", left: "20px", top: "20px"      }, 1000);
    }
    else {
      $("#zPIC").animate({ width: $(this).data('width'), height: $(this).data('height'), }, 1000);
    }
  });
});

关于javascript - 单击更改元素位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25058820/

相关文章:

javascript - JQuery when().done() 无法正常工作

javascript - JQuery 选择器使用值,但属性未知

jquery - 使用 jQuery 删除 css 属性

javascript - 为什么这总是评估为假?

javascript - 如何解析弹出框到文本框的值?

javascript - 切换功能仅在 safari 中工作之前添加分栏符

html - CSSOM 和 DOM 创建是异步的吗?

css - 使用带有 rem = 10px 的 bootstrap 的问题

Javascript HTML > XHTML 转换器

Javascript 在 Photoshop 中隐藏提示