JS拖拽-面向对象拖拽-继承
2024-10-08 15:13:35
//普通拖拽
/*
* 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;
}
最新文章
- 使用python实现栈和队列
- AFNetwork 作用和用法详解
- CSS Reset / Normalize 如何进行样式重置
- Yii框架下使用redis做缓存,读写分离
- HTML5 History对象,Javascript修改地址栏而不刷新页面(二)
- Mysql用户相关操作
- PreparedStatement接口
- CentOS6.8系统下,ecipse下进行编辑操作,意外退出
- MSIL实用指南-IL版hello world
- vscode1.30.1使用的electron3.0.10中的bug
- 找一个数组的最大和的连续子数组(时间复杂度 O(n))
- 当你想要在conda指定的某个环境中安装包的方法
- Java正则表达式使用 | 叠加
- 结构化分析(SA)
- 对STM32库函数中 assert 函数的认知
- java输出重定向
- Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)
- Spring 中好用的泛型操作API
- 分布式流式计算平台——S4
- 上传文件时form表单需要添加的属性
热门文章
- 关于宽搜BFS广度优先搜索的那点事
- The Preliminary Contest for ICPC Asia Xuzhou 2019 K. Center
- SmartRobotControlPlateform——智能机器人控制平台
- 使用SSH客户端远程登录Linux主机
- MongoDB-1 入门
- Linux - 查看端口占用、开放情况
- sso系统登录以及jsonp原理
- 【渗透测试】MS17-010 ";永恒之蓝"; 修复方案
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 线性代数
- Nexus-VDC(Virtual Device Context)