用鼠標改變元素的尺寸。
如需了解更多有關 resizable 交互的細節,請查看 API 文檔 可調整尺寸小部件(Resizable Widget)。
默認功能
在任意的 DOM 元素上啟用 resizable 功能。通過鼠標拖拽右邊或底邊的邊框到所需的寬度或高度。
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>jQuery UI 縮放(Resizable) - 默認功能</title> <link rel="stylesheet" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <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" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <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" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <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" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <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" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <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" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <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" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <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" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <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" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <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" > <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" > <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,可在縮放期間顯示一個半透明的元素,而不是顯示一個實際的元素。
lt;head runat="server">
<title>歡迎登陸~</title>
<script type="text/javascript">
function fullscreen(){
var iWidth = window.screen.availWidth;
var iHeight = window.screen.availHeight;
window.moveTo(0,0);
window.resizeTo(iWidth,iHeight); //登陸后窗體自動最大化
}
</script>
</head>
<body onload="fullscreen()" background="Image/57fddcc7ea661.jpg">
</body>
了點時間寫的,蠻長時間了。個人很喜歡,一段很簡單的代碼,卻能夠實現很多功能。(因為代碼文字呈現沒有格式,難以閱讀,以后小編提供的代碼都以截圖方式呈現,底部有源碼鏈接)。
到底多簡單,先來看代碼
基于jQuery
基于jQuery
拖拽實例圖:
拖拽實例圖
將代碼剝離,只要寫5行就可以實現拖拽了,是不是很簡單:
調用方式
放大、縮小
我們給拖拽增加點功能,支持放大、縮小,先看實例圖:
放大、縮小
將代碼剝離,原先的代碼保留不變,增加一個綁定事件:
放大、縮小
這樣來實現放大、縮小、拖拽是不是很簡單,還能實現很多其他效果,大家慢慢領悟。
原理分析:
放大、縮小、拖拽都離不開在網頁上拖動鼠標,對于前端來說就是 document 的 mousemove,當鼠標在網頁上移動的時候,無時無刻不在觸發 mousemove 事件,當鼠標觸發事件時,什么時候需要執行我們特定的操作,這就是我們要做的了。我在 mousemove 中增加了幾個對象來判定是否進行操作:
move:是否執行觸發操作
move_target:操作的元素對象
move_target.posix:操作對象的坐標
call_down:mousemove的時候的回調函數,傳回來的this指向document
call_up:當鼠標彈起的時候執行的回調函數,傳回來的this指向document
小提示:
簡單的操作,只需要設定 move_target 對象,設置 move_target 的時候不要忘記了 move_target.posix 哦;
復雜的操作可以通過call_down、call_up進行回調操作,這個時候是可以不用設置 move_target 對象的
深入研究
拖拽和放大、縮小實現了,但是有個問題,當我們鼠標點擊并滑動的時候,是會選中文本的,為了避免這個問題,大家可以自行百度
css 阻止文本選中
css 阻止文本選中
網頁的放大、縮小、拖拽事件就研究到這里了,小編不再對如何拓展進行深入講解,一切靠大家自行研究,權當課后作業了?!?/p>
源碼鏈接地址:
http://orzcss.com/posts/d554a392/
本文內容均屬個人原創作品,轉載此文章須附上出處及原文鏈接。
加關注,定時推送,互動精彩多,若你有更好的見解,歡迎留言探討!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。