博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面向对象的拖拽
阅读量:7240 次
发布时间:2019-06-29

本文共 1729 字,大约阅读时间需要 5 分钟。

今天闲来没事写一个简单的面向对象的拖拽,然后在运用继承方法做一个限制大小的拖拽,好下面先写一个面向对象的拖拽。

divclass{width:100px;height:100px;background:#ccc}
me
function Drag(id){    var _this = this;    this.oDiv=document.getElementById(id);    this.oDiv.οnmοusedοwn=function(ev){      _this.fnDown(ev);      return false;    }};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.onmousemove=function(ev){      _this.fnMove(ev);    }    document.onmouseup=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.onmousemove=document.οnmοuseup=null;};

下边是继承 首先新建一个FuDrag()类  新增一个方法

function FuDrag(id){  Drag.call(this,id);//构造函数}for(i in Drag.prototype){  FuDrag.prototype[i] = Drag.prototype[i];//不同于引用,只改变了子集的}FuDrag.prototype.fnMove =function(ev){    var oEvent=ev||event;    l=oEvent.clientX-this.disX;    t=oEvent.clientY-this.disY;    if(l < 0){      l = 0;    }else if(l > document.documentElement.clientWidth-this.oDiv.offsetWidth){      l= document.documentElement.clientWidth-this.oDiv.offsetWidth;    };        if(t <0){      t = 0;    }else if(t > document.documentElement.clientHeight-this.oDiv.offsetHeight){      t= document.documentElement.clientHeight-this.oDiv.offsetHeight    };    this.oDiv.style.left=l+"px";    this.oDiv.style.top=t+"px";  }

然后后在测试一下,我们定义两个div  分别创建不同的对象

window.onload = function(){  new Drag('div1');  new FuDrag('div2')}

 

转载于:https://www.cnblogs.com/xingweb/p/4261910.html

你可能感兴趣的文章
Qt with OpenCascade
查看>>
redis理解
查看>>
ELK大流量日志分析系统搭建
查看>>
Python之路【第二十八篇】:生成器与迭代器
查看>>
2017 Multi-University Training Contest - Team 1
查看>>
C语言遇到的小问题
查看>>
webpack学习笔记--其它配置项
查看>>
霍纳规则解决多项式的求值问题
查看>>
哈夫曼编码测试
查看>>
团队———我们又动了谁的奶酪
查看>>
面向对象的其他知识
查看>>
python中,有关正则表达式re函数:compile、match、search、findall
查看>>
java中函数是值传递还是引用传递?
查看>>
VMWare Server 2.0 安装虚机机网卡驱动找不到
查看>>
python模块之logging模块
查看>>
Cron 表达式详解和案例
查看>>
HDU 3535 AreYouBusy
查看>>
磁盘管理之磁盘组成
查看>>
Page LSNs and Recovery
查看>>
select 0 与select (0)
查看>>