jQuery UI可调整大小的事件
jQuery UI由GUI部件、视觉效果和使用HTML、CSS和jQuery实现的主题组成。 jQuery UI非常适用于为网页构建UI界面。jQuery UI可调整大小的事件是在调整大小的操作中被触发的。
语法:
初始化可调整大小的事件。
$( ".selector" ).resizable({
resize: function( event, ui ) {}
});
- 将一个事件监听器绑定到调整大小事件上。
$( ".selector" ).on( "resize", function( event, ui ) {} );
- 将高度调整限制在27个像素的范围内。
$( ".selector" ).resizable({
resize: function( event, ui ) {
ui.size.height = Math.round( ui.size.height / 27 ) * 27;
}
});
参数:它接受一个有两个参数的回调函数。
event:它接受事件类型值。
ui:它接受对象类型的值,如下图所示。
- element。代表要调整大小的元素的jQuery对象
- helper。代表被调整大小的帮助器的jQuery对象。
- originalElement。代表原始元素的jQuery对象,在它被包装之前。
- originalPosition。在可调整大小之前,以{左,顶}表示的位置。
- originalSize。在可调整大小之前,以{宽度,高度}表示的大小。
- position。当前的位置,表示为{左,顶}。这些值可以被改变以修改元素的位置。对自定义调整大小的逻辑很有用。
- size。当前的尺寸,表示为{宽度,高度}。这些值可以被改变以修改元素的位置。对自定义调整大小的逻辑很有用。
CDN链接:首先,添加你的项目需要的jQuery UI脚本。
<link rel=”stylesheet” href=”//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css”>
<script src=”//code.jquery.com/jquery-1.12.4.js”></script>
<script src=”//code.jquery.com/ui/1.12.1/jquery-ui.js”></script>
例子:这个例子演示了jQuery UI可调整的resize事件。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href=
"//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<script src=
"//code.jquery.com/jquery-1.12.4.js">
</script>
<script src=
"//code.jquery.com/ui/1.12.1/jquery-ui.js">
</script>
<style>
h1 {
color: green;
}
#GFG {
width: 150px;
height: 150px;
background: green;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
</style>
</head>
<body>
<center>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h3>jQuery UI Resizable resize Events</h3>
<div id="log"></div>
<div id="GFG">GeeksforGeeks</div>
</center>
<script>
(document).ready(function () {
("#GFG").resizable({
resize: function (event, ui) {
ui.size.height =
Math.round(ui.size.height / 27) * 27;
}
});
("#GFG").on("resize", function (event, ui) {
("#log").html('Resize operation is going on.');
});
});
</script>
</body>
</html>
输出: