//普通拖拽
/*
* 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;
}

JS拖拽-面向对象拖拽-继承的更多相关文章

  1. F2工作流引擎之-纯JS Web在线可拖拽的流程设计器(八)

          Web纯JS流程设计器无需编程,完全是通过鼠标拖.拉.拽的方式来完成,支持串行.并行.分支.异或分支.M取N路分支.会签.聚合.多重聚合.退回.传阅.转交,都可以非常方便快捷地实现,管理员 ...

  2. js div浮动层拖拽效果代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 纯JS Web在线可拖拽的流程设计器

    F2工作流引擎之-纯JS Web在线可拖拽的流程设计器 Web纯JS流程设计器无需编程,完全是通过鼠标拖.拉.拽的方式来完成,支持串行.并行.分支.异或分支.M取N路分支.会签.聚合.多重聚合.退回. ...

  4. js 实现table表格拖拽和点击表头升降序排序

    js 实现table表格拖拽和点击表头升降序排序,写的比较乱,用的时候可以把其中的一些模块函数提取出来 样式,由于是可拖拽表格,所以样式 table tr th{cursor:move;} js实现 ...

  5. c# 实现文件拖入和拖出(拖拽)

    摘自:http://www.cnblogs.com/eaglet/archive/2009/01/06/1370149.html C# WinForm下一步一步实现文件的拖入和拖出 作者:Eaglet ...

  6. 前端 ---JS中的面向对象

    JS中的面向对象   创建对象的几种常用方式 1.使用Object或对象字面量创建对象 2.工厂模式创建对象 3.构造函数模式创建对象 4.原型模式创建对象 1.使用Object或对象字面量创建对象 ...

  7. python 全栈开发,Day52(关于DOM操作的相关案例,JS中的面向对象,定时器,BOM,client、offset、scroll系列)

    昨日作业讲解: 京东购物车 京东购物车效果: 实现原理: 用2个盒子,就可以完整效果. 先让上面的小盒子向下移动1px,此时就出现了压盖效果.小盒子设置z-index压盖大盒子,将小盒子的下边框去掉, ...

  8. 前端JavaScript(3)-关于DOM操作的相关案例,JS中的面向对象、定时器、BOM、位置信息

    小例子: 京东购物车 京东购物车效果: 实现原理: 用2个盒子,就可以完整效果. 先让上面的小盒子向下移动1px,此时就出现了压盖效果.小盒子设置z-index压盖大盒子,将小盒子的下边框去掉,就可以 ...

  9. Python3 与 C# 面向对象之~继承与多态 Python3 与 C# 面向对象之~封装 Python3 与 NetCore 基础语法对比(Function专栏) [C#]C#时间日期操作 [C#]C#中字符串的操作 [ASP.NET]NTKO插件使用常见问题 我对C#的认知。

    Python3 与 C# 面向对象之-继承与多态   文章汇总:https://www.cnblogs.com/dotnetcrazy/p/9160514.html 目录: 2.继承 ¶ 2.1.单继 ...

随机推荐

  1. 一周搞定模拟电路_P5_基本放电电路记录

    1.原始基本放大电路 2.改变Rb,Rb由200kΩ变为100kΩ 3 4

  2. 交换分区swap和日志系统

    目录 1.使用系统中的某个文件作为swap分区 1.1创建swapfile 1.2格式化swap分区 1.3检查当前swap分区情况 1.4临时启用新建swap分区并查看情况 1.5 关闭新建的swa ...

  3. win server 挂载

    新建服务器角色,选择[NFS服务器]. mount -o nolock \\x.x.x.x.x.x\! z:/*链接到*/

  4. 【译】高级T-SQL进阶系列 (三)【中篇】:理解公共表表达式(CTEs)

    [译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文链接:传送门. 一个简单的CTE例子 如前所述,CTE‘s提供给你了一个方法来更容易的书写复杂的代码以提高其可读性.假设你有列表 ...

  5. call深入理解

    function fn1() { console.log(1); } function fn2() { console.log(2); } fn1.call(fn2); // 1 fn1.call.c ...

  6. MongoDB - 运行

    运行 mongod --dbpath (mongod is the "Mongo Daemon") 在shell用mongo或者用studio 3t可视化连接 创建用户 db.cr ...

  7. Google Earth Engine学习资源分享

    最近在学习Google Earth Engine的使用,发现这个平台确实是一个非常好用.非常强大的平台.在GEE官网上找到了一些中文的学习资料,现在搬运过来分享给大家共同学习.教程分为两个部分 教程一 ...

  8. PHP将json或对象转成数组

    今天老大突然给了我一个小任务,给我一个txt文件,里边是很多的json字串,要求将这些字串转换成php中的数组: 于是开足火力,用了将进5分钟的时间完成了任务,代码如下: $jsonStr = fil ...

  9. springmvc项目的搭建

    springmvc替代servlet的工作 Servlet - Springmvc        jsp ->Servlet (Springmvc)->Jsp springmvc配置文件 ...

  10. Services: ARP Caching

    Online Help for Cisco IOS Release 12.2(15)JA Services: ARP Caching   ARP caching on the access point ...