图片拖拽面向对象写法-2

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
     #box1{
     width: 100px;
     height: 100px;
     background-color: #ffff00;
     position: absolute;
     }
     #box2{
     width: 100px;
     height: 100px;
     background-color: #0000ff;
     position: absolute;
     left: 120px;
     }
</style>

<script>
     window.οnlοad=function(){
     new Drag("box1")
     new Drag("box2")

     }
//第四步:全局变量变成属性 该加this加this
// var oDiv=null;
// var disX=0;
// var disY=0;
//第三步:Window.onload改成构造函数
function Drag(id){
this.disX=0
this.disY=0
var _this=this;
this.oDiv=document.getElementById(id);
this.oDiv.οnmοusedοwn=function(){
//第六步 修改当前的从属关系
_this.fnDown()
};
}
//第五步:把函数改为Drag原型方法
Drag.prototype.fnDown=function(ev){
var _this=this;
var oEvent=ev||event;
this.disX = oEvent.clientX-this.oDiv.offsetLeft;
this.disY = oEvent.clientY-this.oDiv.offsetTop;
document.οnmοusemοve=function(){
_this.fnMove()
}
document.οnmοuseup=function(){
_this.fnUp()
}
}
Drag.prototype.fnMove=function(ev){
var oEvent=ev||event;
this.oDiv.style.left=oEvent.clientX-this.disX+'px';
this.oDiv.style.top=oEvent.clientY-this.disY+'px';
}
Drag.prototype.fnUp=function(){
document.οnmοusemοve=null;
document.οnmοuseup=null;
}
</script>
</head>
<body>
<div id="box1"></div>
<div id="box2"></div>
</body>
</html>

转载于:https://www.cnblogs.com/mylove0/p/7464913.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值