首页 技术 正文
技术 2022年11月15日
0 收藏 323 点赞 4,353 浏览 1700 个字

“元素拖拽改变大小”其实和”元素拖拽”一个原理,只是所动态改变的对象不同而已,主要在于 top、left、width、height 的运用,相对实现起来也非常容易。以下附出源码原型,弄明白了原理再扩展其他实际应用,思路就变得简单、清晰得多了。先来看看效果:

下面是 JavaScript Code

view 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>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844