课程表

jQuery UI 基础

jQuery UI 主题

jQuery UI 部件库

jQuery UI 参考手册

jQuery UI 实例

工具箱
速查手册

缩放(Resizable)

当前位置:免费教程 » JS/JS库/框架 » jQuery UI

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

如需了解更多有关 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. <div id="resizable" class="ui-widget-content">
  22. <h3 class="ui-widget-header">缩放(Resizable)</h3>
  23. </div>
  24. </body>
  25. </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. <div id="resizable" class="ui-widget-content">
  25. <h3 class="ui-widget-header">动画</h3>
  26. </div>
  27. </body>
  28. </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. <div id="container" class="ui-widget-content">
  26. <h3 class="ui-widget-header">限制</h3>
  27. <div id="resizable" class="ui-state-active">
  28. <h3 class="ui-widget-header">缩放(Resizable)</h3>
  29. </div>
  30. </div>
  31. </body>
  32. </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. $( "#resizable2" ).resizable({
  20. distance: 40
  21. });
  22. });
  23. </script>
  24. </head>
  25. <body>
  26. <h3 class="docs">时间延迟 (ms):</h3>
  27. <div id="resizable" class="ui-widget-content">
  28. <h3 class="ui-widget-header">时间</h3>
  29. </div>
  30. <h3 class="docs">距离延迟 (px):</h3>
  31. <div id="resizable2" class="ui-widget-content">
  32. <h3 class="ui-widget-header">距离</h3>
  33. </div>
  34. </body>
  35. </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. <div id="resizable" class="ui-widget-content">
  25. <h3 class="ui-widget-header">助手</h3>
  26. </div>
  27. </body>
  28. </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. <div id="resizable" class="ui-widget-content">
  27. <h3 class="ui-widget-header">放大/缩小</h3>
  28. </div>
  29. </body>
  30. </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. <div id="resizable" class="ui-widget-content">
  24. <h3 class="ui-widget-header">保持纵横比</h3>
  25. </div>
  26. </body>
  27. </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. <div id="resizable" class="ui-widget-content">
  24. <h3 class="ui-widget-header">网格</h3>
  25. </div>
  26. </body>
  27. </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. <div id="resizable" class="ui-widget-header">
  27. <h3 class="ui-state-active">缩放</h3>
  28. </div>
  29. <div id="also" class="ui-widget-content">
  30. <h3 class="ui-widget-header">同步缩放</h3>
  31. </div>
  32. </body>
  33. </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. <textarea id="resizable" rows="5" cols="20"></textarea>
  25. </body>
  26. </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. <div id="resizable" class="ui-widget-content">
  25. <h3 class="ui-widget-header">Ghost</h3>
  26. </div>
  27. </body>
  28. </html>

我要试一下

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号