//普通拖拽
/*
* CSS
*/
#div1{ width:100px; height:100px; position:absolute; background:red; cursor:move;} /*
*HTML
*/
<div id="div1"></div> /*
*JavaScript
*/
window.onload = function (){
var oDiv = document.getElementById("div1");
oDiv.onmousedown = function(ev){
var oEvent = ev || event; //事件处理函数 ev是FF
var disX = oEvent.clientX - oDiv.offsetLeft;
var disY = oEvent.clientY - oDiv.offsetTop;
doucment.onmousemove = function (ev){
var oEvent = ev || event;
var l = oEvent.clientX - this.disX;
var t = oEvent.clientY - this.disY;
oDiv.style.left = l + "px";
oDiv.style.top = t + "px";
document.title = l + " , " + t; //坐标
return false; //阻止火狐默认事件
}
document.onmouseup = function (){
document.onmousemove = null;
document.onmouseup = null;
}
}
}

 //面向对象拖拽
/*
*CSS
*/
#div1{ width:100px; height:100px; position:absolute; background:red; cursor:move;}
#div2{ width:100px; height:100px; position:absolute; background:yellow; cursor:pointer;} /*
*HTML
*/
<div id="div1"></div>
<div id="div2"></div> /*
*Javascript
*/
window.onload = function (){ new Darg("div1");
new Darg("div2"); }
function Darg(id){
var _this = this;
this.oDiv = document.getElementById(id);
this.oDiv.onmousedown = function (ev){
_this.ondown(ev);
}
}
Darg.prototype.ondown = 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.onmove(ev);
}
document.onmouseup = function (){
_this.onup();
}
}
Darg.prototype.onmove = function(ev){
var oEvent = ev || event;
var l = oEvent.clientX - this.disX;
var t = oEvent.clientY - this.disY;
this.oDiv.style.left = l + "px";
this.oDiv.style.top = t + "px";
document.title = l + " , " + t;
return false;
}
Darg.prototype.onup = function(){
document.onmousemove = null;
document.onmouseup = null;
}

 //面向对象拖拽-继承
/*
*CSS
*/
#div1{ width:100px; height:100px; position:absolute; background:red; cursor:move;}
#div2{ width:100px; height:100px; position:absolute; background:yellow; cursor:pointer;} /*
*HTML
*/
<div id="div1"></div>
<div id="div2"></div> /*
*Javascript
*/
window.onload = function (){ new Darg("div1");
new extendsDarg("div2"); }
//面向对象拖拽开始
function Darg(id){
var _this = this;
this.oDiv = document.getElementById(id);
this.oDiv.onmousedown = function (ev){
_this.ondown(ev);
}
}
Darg.prototype.ondown = 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.onmove(ev);
}
document.onmouseup = function (){
_this.onup();
}
}
Darg.prototype.onmove = function(ev){
var oEvent = ev || event;
var l = oEvent.clientX - this.disX;
var t = oEvent.clientY - this.disY;
this.oDiv.style.left = l + "px";
this.oDiv.style.top = t + "px";
document.title = l + " , " + t;
return false;
}
Darg.prototype.onup = function(){
document.onmousemove = null;
document.onmouseup = null;
}
//面向对象拖拽结束 //这里开始继承
function extendsDarg(id){
Darg.call(this,id);
}
for(i in Darg.prototype){
extendsDarg.prototype[i] = Darg.prototype[i];
}
extendsDarg.prototype.onmove = function(ev){
var oEvent = ev || event;
var l = oEvent.clientX - this.disX;
var 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";
document.title = l + " , " + t;
return false;
}

最新文章

  1. 使用python实现栈和队列
  2. AFNetwork 作用和用法详解
  3. CSS Reset / Normalize 如何进行样式重置
  4. Yii框架下使用redis做缓存,读写分离
  5. HTML5 History对象,Javascript修改地址栏而不刷新页面(二)
  6. Mysql用户相关操作
  7. PreparedStatement接口
  8. CentOS6.8系统下,ecipse下进行编辑操作,意外退出
  9. MSIL实用指南-IL版hello world
  10. vscode1.30.1使用的electron3.0.10中的bug
  11. 找一个数组的最大和的连续子数组(时间复杂度 O(n))
  12. 当你想要在conda指定的某个环境中安装包的方法
  13. Java正则表达式使用 | 叠加
  14. 结构化分析(SA)
  15. 对STM32库函数中 assert 函数的认知
  16. java输出重定向
  17. Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)
  18. Spring 中好用的泛型操作API
  19. 分布式流式计算平台——S4
  20. 上传文件时form表单需要添加的属性

热门文章

  1. 关于宽搜BFS广度优先搜索的那点事
  2. The Preliminary Contest for ICPC Asia Xuzhou 2019 K. Center
  3. SmartRobotControlPlateform——智能机器人控制平台
  4. 使用SSH客户端远程登录Linux主机
  5. MongoDB-1 入门
  6. Linux - 查看端口占用、开放情况
  7. sso系统登录以及jsonp原理
  8. 【渗透测试】MS17-010 &quot;永恒之蓝&quot; 修复方案
  9. 吴裕雄--天生自然Numpy库学习笔记:NumPy 线性代数
  10. Nexus-VDC(Virtual Device Context)