//普通拖拽
/*
* 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. 【代码学习】PYTHON 进程

    一.进程和程序的区别 编写完毕的代码,在没有运行的时候称之为程序 正在运行的代码,称之为进程 进程除了包含的代码意外,还需要运行的环境等 二.fork import os import time # ...

  2. Spring Boot 操作 Excel

    Excel 在日常操作中经常使用到,Spring Boot 中使用 POI 操作 Excel 本项目源码 github 下载 1 新建 Spring Boot Maven 示例工程项目 注意:本示例是 ...

  3. 吴裕雄 python 神经网络——TensorFlow 使用卷积神经网络训练和预测MNIST手写数据集

    import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_dat ...

  4. Go标准库之Log

      文章引用自 Go语言标准库log介绍 无论是软件开发的调试阶段还是软件上线之后的运行阶段,日志一直都是非常重要的一个环节,我们也应该养成在程序中记录日志的好习惯. log Go语言内置的log包实 ...

  5. C语言:将s所指字符串中下标为偶数同时ASCII值为奇数的字符删去,-将a所指字符串中的字符和b所指字符串中的字符的顺序交叉,-将形参s所指字符串中的所有数字字符顺序前移,

    //函数fun功能:将s所指字符串中下标为偶数同时ASCII值为奇数的字符删去,s所指串中剩余的字符形成的新串放在t所指的数组中. #include <stdio.h> #include ...

  6. windows server 2016系统激活

    slmgr /ipk CB7KF-BWN84-R7R2Y-793K2-8XDDG slmgr /skms kms.03k.org slmgr /ato

  7. 工具 - 正则Cheat sheet

  8. Linux - Windows下的Linux工具

    1. putty, 2. winscp(可下载文件) 3. SecureCRT

  9. redis的使用1

    学Linux已经将近一个月了,Linux中讲到的redis的使用,到现在还不回具体的使用在php中,今天周末,于是想把redis的使用搞懂. 网上的资料不算多,但还需要硬着头皮学.其中找到这样一篇关于 ...

  10. HDU1540 Turnal Warfare

    线段树保存每个区间的左边最大连续长度和右边最大连续长度~ #include<cstdio> #include<cstring> #include<algorithm> ...