首页 技术 正文
技术 2022年11月15日
0 收藏 925 点赞 3,781 浏览 1336 个字

前面的话

  在之前的博客中,拖拽的实现使用了面向过程的写法。本文将以面向对象的写法来实现拖拽

写法

<style>
.test{height: 50px;width: 50px;background-color: pink;position:absolute;}
#test2{left:60px;background-color: lightblue;}
</style>
</head>
<body>
<div id="test1" class="test"></div>
<div id="test2" class="test"></div>
<script>
function Drag(obj){
this.obj= obj;
}
Drag.prototype.init = function(){
var that = this;
this.obj.onmousedown = function(e){
e = e || event;
that.fnDown(e);
document.onmousemove = function(e){
e = e || event;
that.fnMove(e);
}
document.onmouseup = function(){
that.fnUp();
}
return false;
}
};
Drag.prototype.fnDown = function(e){
this.disX = e.clientX - this.obj.offsetLeft;
this.disY = e.clientY - this.obj.offsetTop;
}
Drag.prototype.fnMove = function(e){
this.obj.style.left = e.clientX - this.disX + 'px';
this.obj.style.top = e.clientY - this.disY + 'px';
}
Drag.prototype.fnUp = function(){
document.onmousemove = document.onmouseup = null;
}
function ChildDrag(obj){
Drag.call(this,obj);
}
if(!Object.create){
  Object.create = function(proto){
    function F(){};
    F.prototype = proto;
    return new F;
  }
}
ChildDrag.prototype = Object.create(Drag.prototype);
ChildDrag.prototype.constructor = ChildDrag;
var drag1 = new Drag(test1);
drag1.init();
ChildDrag.prototype.fnMove = function(e){
var L = e.clientX - this.disX;
var T = e.clientY - this.disY;
if(L < 0){L = 0;}
if(T < 0){T = 0;}
this.obj.style.left = L + 'px';
this.obj.style.top = T + 'px';
}
var drag2 = new ChildDrag(test2);
drag2.init();
</script>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849