缩放(Resizable)


jQuery UI 实例 - 缩放(Resizable)

使用鼠标改变元素的尺寸。

如需了解更多有关 resizable 交互的细节,请查看 API 文档 可调整尺寸小部件(Resizable Widget)。

默认功能

在任意的 DOM 元素上启用 resizable 功能。通过鼠标拖拽右边或底边的边框到所需的宽度或高度。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 缩放(Resizable) - 默认功能</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #resizable { width: 150px; height: 150px; padding: 0.5em; }
  12.   #resizable h3 { text-align: center; margin: 0; }
  13.   </style>
  14.   <script>
  15.   $(function() {
  16.     $( "#resizable" ).resizable();
  17.   });
  18.   </script>
  19. </head>
  20. <body>
  21.  
  22. <div id="resizable" class="ui-widget-content">
  23.   <h3 class="ui-widget-header">缩放(Resizable)</h3>
  24. </div>
  25.  
  26.  
  27. </body>
  28. </html>

动画

使用 animate 选项(布尔值)使缩放行为动画化。当该选项设置为 true 时,拖拽轮廓到所需的位置,元素会在拖拽停止时以动画形式调整到该尺寸。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 缩放(Resizable) - 动画</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #resizable { width: 150px; height: 150px; padding: 0.5em; }
  12.   #resizable h3 { text-align: center; margin: 0; }
  13.   .ui-resizable-helper { border: 1px dotted gray; }
  14.   </style>
  15.   <script>
  16.   $(function() {
  17.     $( "#resizable" ).resizable({
  18.       animate: true
  19.     });
  20.   });
  21.   </script>
  22. </head>
  23. <body>
  24.  
  25. <div id="resizable" class="ui-widget-content">
  26.   <h3 class="ui-widget-header">动画</h3>
  27. </div>
  28.  
  29.  
  30. </body>
  31. </html>

限制缩放区域

定义缩放区域的边界。使用 containment 选项来指定一个父级的 DOM 元素或一个 jQuery 选择器,比如 'document.'。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 缩放(Resizable) - 限制缩放区域</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #container { width: 300px; height: 300px; }
  12.   #container h3 { text-align: center; margin: 0; margin-bottom: 10px; }
  13.   #resizable { background-position: top left; width: 150px; height: 150px; }
  14.   #resizable, #container { padding: 0.5em; }
  15.   </style>
  16.   <script>
  17.   $(function() {
  18.     $( "#resizable" ).resizable({
  19.       containment: "#container"
  20.     });
  21.   });
  22.   </script>
  23. </head>
  24. <body>
  25.  
  26. <div id="container" class="ui-widget-content">
  27.   <h3 class="ui-widget-header">限制</h3>
  28.   <div id="resizable" class="ui-state-active">
  29.     <h3 class="ui-widget-header">缩放(Resizable)</h3>
  30.   </div>
  31. </div>
  32.  
  33.  
  34. </body>
  35. </html>

延迟开始

通过 delay 选项设置延迟开始缩放的毫秒数。通过 distance 选项设置光标被按下且拖拽指定像素后才允许缩放。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 缩放(Resizable) - 延迟开始</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #resizable, #resizable2 { width: 150px; height: 150px; padding: 0.5em; }
  12.   #resizable h3, #resizable2 h3 { text-align: center; margin: 0; }
  13.   </style>
  14.   <script>
  15.   $(function() {
  16.     $( "#resizable" ).resizable({
  17.       delay: 1000
  18.     });
  19.  
  20.     $( "#resizable2" ).resizable({
  21.       distance: 40
  22.     });
  23.   });
  24.   </script>
  25. </head>
  26. <body>
  27.  
  28. <h3 class="docs">时间延迟 (ms):</h3>
  29. <div id="resizable" class="ui-widget-content">
  30.   <h3 class="ui-widget-header">时间</h3>
  31. </div>
  32.  
  33. <h3 class="docs">距离延迟 (px):</h3>
  34. <div id="resizable2" class="ui-widget-content">
  35.   <h3 class="ui-widget-header">距离</h3>
  36. </div>
  37.  
  38.  
  39. </body>
  40. </html>

助手

通过设置 helper 选项为一个 CSS class,当缩放时只显示元素的轮廓。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 缩放(Resizable) - 助手</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #resizable { width: 150px; height: 150px; padding: 0.5em; }
  12.   #resizable h3 { text-align: center; margin: 0; }
  13.   .ui-resizable-helper { border: 2px dotted #00F; }
  14.   </style>
  15.   <script>
  16.   $(function() {
  17.     $( "#resizable" ).resizable({
  18.       helper: "ui-resizable-helper"
  19.     });
  20.   });
  21.   </script>
  22. </head>
  23. <body>
  24.  
  25. <div id="resizable" class="ui-widget-content">
  26.   <h3 class="ui-widget-header">助手</h3>
  27. </div>
  28.  
  29.  
  30. </body>
  31. </html>

最大/最小尺寸

使用 maxHeightmaxWidthminHeightminWidth 选项限制 resizable 元素的最大或最小高度或宽度。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 缩放(Resizable) - 最大/最小尺寸</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #resizable { width: 200px; height: 150px; padding: 5px; }
  12.   #resizable h3 { text-align: center; margin: 0; }
  13.   </style>
  14.   <script>
  15.   $(function() {
  16.     $( "#resizable" ).resizable({
  17.       maxHeight: 250,
  18.       maxWidth: 350,
  19.       minHeight: 150,
  20.       minWidth: 200
  21.     });
  22.   });
  23.   </script>
  24. </head>
  25. <body>
  26.  
  27. <div id="resizable" class="ui-widget-content">
  28.   <h3 class="ui-widget-header">放大/缩小</h3>
  29. </div>
  30.  
  31.  
  32. </body>
  33. </html>

保持纵横比

保持现有的纵横比或设置一个新的纵横比来限制缩放比例。设置 aspectRatio 选项为 true,且可选地传递一个新的比率(比如,4/3)。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 缩放(Resizable) - 保持纵横比</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #resizable { width: 160px; height: 90px; padding: 0.5em; }
  12.   #resizable h3 { text-align: center; margin: 0; }
  13.   </style>
  14.   <script>
  15.   $(function() {
  16.     $( "#resizable" ).resizable({
  17.       aspectRatio: 16 / 9
  18.     });
  19.   });
  20.   </script>
  21. </head>
  22. <body>
  23.  
  24. <div id="resizable" class="ui-widget-content">
  25.   <h3 class="ui-widget-header">保持纵横比</h3>
  26. </div>
  27.  
  28.  
  29. </body>
  30. </html>

对齐到网格

对齐 resizable 元素到网格。通过 grid 选项设置网格单元的尺寸(以像素为单位的高度和宽度)。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 缩放(Resizable) - 对齐到网格</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #resizable { width: 150px; height: 150px; padding: 0.5em; }
  12.   #resizable h3 { text-align: center; margin: 0; }
  13.   </style>
  14.   <script>
  15.   $(function() {
  16.     $( "#resizable" ).resizable({
  17.       grid: 50
  18.     });
  19.   });
  20.   </script>
  21. </head>
  22. <body>
  23.  
  24. <div id="resizable" class="ui-widget-content">
  25.   <h3 class="ui-widget-header">网格</h3>
  26. </div>
  27.  
  28.  
  29. </body>
  30. </html>

同步缩放

通过点击并拖拽一个元素的边来同时调整多个元素的尺寸。给 alsoResize 选项传递一个共享的选择器。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 缩放(Resizable) - 同步缩放</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #resizable { background-position: top left; }
  12.   #resizable, #also { width: 150px; height: 120px; padding: 0.5em; }
  13.   #resizable h3, #also h3 { text-align: center; margin: 0; }
  14.   #also { margin-top: 1em; }
  15.   </style>
  16.   <script>
  17.   $(function() {
  18.     $( "#resizable" ).resizable({
  19.       alsoResize: "#also"
  20.     });
  21.     $( "#also" ).resizable();
  22.   });
  23.   </script>
  24. </head>
  25. <body>
  26.  
  27. <div id="resizable" class="ui-widget-header">
  28.   <h3 class="ui-state-active">缩放</h3>
  29. </div>
  30.  
  31. <div id="also" class="ui-widget-content">
  32.   <h3 class="ui-widget-header">同步缩放</h3>
  33. </div>
  34.  
  35.  
  36. </body>
  37. </html>

文本框

可缩放的文本框。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 缩放(Resizable) - 文本框</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   .ui-resizable-se {
  12.     bottom: 17px;
  13.   }
  14.   </style>
  15.   <script>
  16.   $(function() {
  17.     $( "#resizable" ).resizable({
  18.       handles: "se"
  19.     });
  20.   });
  21.   </script>
  22. </head>
  23. <body>
  24.  
  25. <textarea id="resizable" rows="5" cols="20"></textarea>
  26.  
  27.  
  28. </body>
  29. </html>

视觉反馈

通过设置 ghost 选项为 true,可在缩放期间显示一个半透明的元素,而不是显示一个实际的元素。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 缩放(Resizable) - 视觉反馈</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.   #resizable { width: 150px; height: 150px; padding: 0.5em; }
  12.   #resizable h3 { text-align: center; margin: 0; }
  13.   .ui-resizable-ghost { border: 1px dotted gray; }
  14.   </style>
  15.   <script>
  16.   $(function() {
  17.     $( "#resizable" ).resizable({
  18.       ghost: true
  19.     });
  20.   });
  21.   </script>
  22. </head>
  23. <body>
  24.  
  25. <div id="resizable" class="ui-widget-content">
  26.   <h3 class="ui-widget-header">Ghost</h3>
  27. </div>
  28.  
  29.  
  30. </body>
  31. </html>