jQuery调整大小函数只适用于窗口

jQuery resize function does only work on window?

本文关键字:适用于 窗口 函数 调整 jQuery      更新时间:2023-09-26

我想知道jQuery .resize()函数是否只适用于(a)窗口?我不能使用这个函数来检测某个元素是否调整大小吗?

例如,下面的JSFiddle现在不起作用了吗?但是,真的能让它发挥作用吗?

$(document).ready(function() {
  /* Does not work for me: */
  $(".foo").resize(function() {
    alert("yes!");
  });
  /* Does work for me: */
  $(window).resize(function() {
    alert("yes!");
  });
});
.foo {
  border: 2px solid;
  padding: 20px;
  width: 100px;
  ;
  resize: both;
  overflow: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p><b>Note:</b> Internet Explorer does not support the resize property.</p>
<div class="foo">
  Drag the corner(s) around.
</div>

你是对的,它只适用于Window。如果窗口大小发生变化(或者在某些手机上滚动过远),浏览器会触发调整大小事件。您必须创建一些可以监控单个元素大小并触发自定义事件的东西。

API中的触发器页面包含一些关于触发自定义事件的信息。

是的,使用这个插件是可能的JqueryUI可调整大小,代码就像一样简单

$( ".foo" ).resizable();

下方的示例演示

$(document).ready(function() {
	$("#resize-me").resizable();
});
#resize-me
{
  height: 150px;
  width: 150px;
  border:2px solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script   src="https://code.jquery.com/ui/1.12.0-rc.2/jquery-ui.min.js"   integrity="sha256-55Jz3pBCF8z9jBO1qQ7cIf0L+neuPTD1u7Ytzrp2dqo="   crossorigin="anonymous"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<div id="resize-me">
 Resize me
</div>