JQuery拖拽改變元素的尺寸

來源:互聯網
上載者:User

標籤:style   http   io   ar   java   sp   div   on   cti   

"元素拖拽改變大小"其實和"元素拖拽"一個原理,只是所動態改變的對象不同而已,主要在於 top、left、width、height 的運用,相對實現起來也非常容易。以下附出源碼原型,弄明白了原理再擴充其他實際應用,思路就變得簡單、清晰得多了。先來看看效果:塔河縣臧清機械

下面是 JavaScript Codeview source print?
01 <script type="text/javascript">
02     /*
03      * jQuery.Resize by wuxinxi007
04      * Date: 2011-5-14
05      */
06     $(function(){
07         //綁定需要拖拽改變大小的元素對象
08         bindResize(document.getElementById(‘test‘));
09     });
10    
11     function bindResize(el){
12         //初始化參數
13         var els = el.style,
14             //滑鼠的 X 和 Y 軸座標
15             x = y = 0;
16         //邪惡的食指
17         $(el).mousedown(function(e){
18             //按下元素後,計算當前滑鼠與對象計算後的座標
19             x = e.clientX - el.offsetWidth,
20             y = e.clientY - el.offsetHeight;
21             //在支援 setCapture 做些東東
22             el.setCapture ? (
23                 //捕捉焦點
24                 el.setCapture(),
25                 //設定事件
26                 el.onmousemove = function(ev){
27                     mouseMove(ev || event)
28                 },
29                 el.onmouseup = mouseUp
30             ) : (
31                 //綁定事件
32                 $(document).bind("mousemove",mouseMove).bind("mouseup",mouseUp)
33             )
34             //防止預設事件發生
35             e.preventDefault()
36         });
37         //移動事件
38         function mouseMove(e){
39             //宇宙超級無敵運算中...
40             els.width = e.clientX - x + ‘px‘,
41             els.height = e.clientY - y + ‘px‘
42         }
43         //停止事件
44         function mouseUp(){
45             //在支援 releaseCapture 做些東東
46             el.releaseCapture ? (
47                 //釋放焦點
48                 el.releaseCapture(),
49                 //移除事件
50                 el.onmousemove = el.onmouseup = null
51             ) : (
52                 //卸載事件
53                 $(document).unbind("mousemove", mouseMove).unbind("mouseup", mouseUp)
54             )
55         }
56     }
57 </script>

JQuery拖拽改變元素的尺寸

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.