jQuery UI 實例 - 縮放(Resizable)
使用滑鼠改變元素的尺寸。
如需瞭解更多有關 resizable 交互的細節,請查看 API 文檔 可調整尺寸小部件(Resizable Widget)。
默認功能
在任意的 DOM 元素上啟用 resizable 功能。通過滑鼠拖拽右邊或底邊的邊框到所需的寬度或高度。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 默認功能</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> #resizable { width: 150px; height: 150px; padding: 0.5em; } #resizable h3 { text-align: center; margin: 0; } </style> <script> $(function() { $( "#resizable" ).resizable(); }); </script> </head> <body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">縮放(Resizable)</h3> </div> </body> </html>
動畫
使用 animate
選項(布爾值)使縮放行為動畫化。當該選項設置為 true 時,拖拽輪廓到所需的位置,元素會在拖拽停止時以動畫形式調整到該尺寸。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 動畫</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> #resizable { width: 150px; height: 150px; padding: 0.5em; } #resizable h3 { text-align: center; margin: 0; } .ui-resizable-helper { border: 1px dotted gray; } </style> <script> $(function() { $( "#resizable" ).resizable({ animate: true }); }); </script> </head> <body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">動畫</h3> </div> </body> </html>
限制縮放區域
定義縮放區域的邊界。使用 containment
選項來指定一個父級的 DOM 元素或一個 jQuery 選擇器,比如 'document.'。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 限制縮放區域</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> #container { width: 300px; height: 300px; } #container h3 { text-align: center; margin: 0; margin-bottom: 10px; } #resizable { background-position: top left; width: 150px; height: 150px; } #resizable, #container { padding: 0.5em; } </style> <script> $(function() { $( "#resizable" ).resizable({ containment: "#container" }); }); </script> </head> <body> <div id="container" class="ui-widget-content"> <h3 class="ui-widget-header">限制</h3> <div id="resizable" class="ui-state-active"> <h3 class="ui-widget-header">縮放(Resizable)</h3> </div> </div> </body> </html>
延遲開始
通過 delay
選項設置延遲開始縮放的毫秒數。通過 distance
選項設置游標被按下且拖拽指定像素後才允許縮放。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 延遲開始</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> #resizable, #resizable2 { width: 150px; height: 150px; padding: 0.5em; } #resizable h3, #resizable2 h3 { text-align: center; margin: 0; } </style> <script> $(function() { $( "#resizable" ).resizable({ delay: 1000 }); $( "#resizable2" ).resizable({ distance: 40 }); }); </script> </head> <body> <h3 class="docs">時間延遲 (ms):</h3> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">時間</h3> </div> <h3 class="docs">距離延遲 (px):</h3> <div id="resizable2" class="ui-widget-content"> <h3 class="ui-widget-header">距離</h3> </div> </body> </html>
助手
通過設置 helper
選項為一個 CSS class,當縮放時只顯示元素的輪廓。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 助手</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> #resizable { width: 150px; height: 150px; padding: 0.5em; } #resizable h3 { text-align: center; margin: 0; } .ui-resizable-helper { border: 2px dotted #00F; } </style> <script> $(function() { $( "#resizable" ).resizable({ helper: "ui-resizable-helper" }); }); </script> </head> <body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">助手</h3> </div> </body> </html>
最大/最小尺寸
使用 maxHeight
、maxWidth
、minHeight
和 minWidth
選項限制 resizable 元素的最大或最小高度或寬度。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 最大/最小尺寸</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> #resizable { width: 200px; height: 150px; padding: 5px; } #resizable h3 { text-align: center; margin: 0; } </style> <script> $(function() { $( "#resizable" ).resizable({ maxHeight: 250, maxWidth: 350, minHeight: 150, minWidth: 200 }); }); </script> </head> <body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">放大/縮小</h3> </div> </body> </html>
保持縱橫比
保持現有的縱橫比或設置一個新的縱橫比來限制縮放比例。設置 aspectRatio
選項為 true,且可選地傳遞一個新的比率(比如,4/3)。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 保持縱橫比</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> #resizable { width: 160px; height: 90px; padding: 0.5em; } #resizable h3 { text-align: center; margin: 0; } </style> <script> $(function() { $( "#resizable" ).resizable({ aspectRatio: 16 / 9 }); }); </script> </head> <body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">保持縱橫比</h3> </div> </body> </html>
對齊到網格
對齊 resizable 元素到網格。通過 grid
選項設置網格單元的尺寸(以像素為單位的高度和寬度)。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 對齊到網格</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> #resizable { width: 150px; height: 150px; padding: 0.5em; } #resizable h3 { text-align: center; margin: 0; } </style> <script> $(function() { $( "#resizable" ).resizable({ grid: 50 }); }); </script> </head> <body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">網格</h3> </div> </body> </html>
同步縮放
通過點擊並拖拽一個元素的邊來同時調整多個元素的尺寸。給 alsoResize
選項傳遞一個共用的選擇器。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 同步縮放</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> #resizable { background-position: top left; } #resizable, #also { width: 150px; height: 120px; padding: 0.5em; } #resizable h3, #also h3 { text-align: center; margin: 0; } #also { margin-top: 1em; } </style> <script> $(function() { $( "#resizable" ).resizable({ alsoResize: "#also" }); $( "#also" ).resizable(); }); </script> </head> <body> <div id="resizable" class="ui-widget-header"> <h3 class="ui-state-active">縮放</h3> </div> <div id="also" class="ui-widget-content"> <h3 class="ui-widget-header">同步縮放</h3> </div> </body> </html>
文本框
可縮放的文本框。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 文本框</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> .ui-resizable-se { bottom: 17px; } </style> <script> $(function() { $( "#resizable" ).resizable({ handles: "se" }); }); </script> </head> <body> <textarea id="resizable" rows="5" cols="20"></textarea> </body> </html>
視覺回饋
通過設置 ghost
選項為 true,可在縮放期間顯示一個半透明的元素,而不是顯示一個實際的元素。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 視覺回饋</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.9.1.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> #resizable { width: 150px; height: 150px; padding: 0.5em; } #resizable h3 { text-align: center; margin: 0; } .ui-resizable-ghost { border: 1px dotted gray; } </style> <script> $(function() { $( "#resizable" ).resizable({ ghost: true }); }); </script> </head> <body> <div id="resizable" class="ui-widget-content"> <h3 class="ui-widget-header">Ghost</h3> </div> </body> </html>