1. //普通拖拽
  2. /*
  3. * CSS
  4. */
  5. #div1{ width:100px; height:100px; position:absolute; background:red; cursor:move;}
  6.  
  7. /*
  8. *HTML
  9. */
  10. <div id="div1"></div>
  11.  
  12. /*
  13. *JavaScript
  14. */
  15. window.onload = function (){
  16. var oDiv = document.getElementById("div1");
  17. oDiv.onmousedown = function(ev){
  18. var oEvent = ev || event; //事件处理函数 ev是FF
  19. var disX = oEvent.clientX - oDiv.offsetLeft;
  20. var disY = oEvent.clientY - oDiv.offsetTop;
  21. doucment.onmousemove = function (ev){
  22. var oEvent = ev || event;
  23. var l = oEvent.clientX - this.disX;
  24. var t = oEvent.clientY - this.disY;
  25. oDiv.style.left = l + "px";
  26. oDiv.style.top = t + "px";
  27. document.title = l + " , " + t; //坐标
  28. return false; //阻止火狐默认事件
  29. }
  30. document.onmouseup = function (){
  31. document.onmousemove = null;
  32. document.onmouseup = null;
  33. }
  34. }
  35. }

  1. //面向对象拖拽
  2. /*
  3. *CSS
  4. */
  5. #div1{ width:100px; height:100px; position:absolute; background:red; cursor:move;}
  6. #div2{ width:100px; height:100px; position:absolute; background:yellow; cursor:pointer;}
  7.  
  8. /*
  9. *HTML
  10. */
  11. <div id="div1"></div>
  12. <div id="div2"></div>
  13.  
  14. /*
  15. *Javascript
  16. */
  17. window.onload = function (){
  18.  
  19. new Darg("div1");
  20. new Darg("div2");
  21.  
  22. }
  23. function Darg(id){
  24. var _this = this;
  25. this.oDiv = document.getElementById(id);
  26. this.oDiv.onmousedown = function (ev){
  27. _this.ondown(ev);
  28. }
  29. }
  30. Darg.prototype.ondown = function(ev){
  31. var _this = this;
  32. var oEvent = ev || event;
  33. this.disX = oEvent.clientX - this.oDiv.offsetLeft;
  34. this.disY = oEvent.clientY - this.oDiv.offsetTop;
  35. document.onmousemove = function (ev){
  36. _this.onmove(ev);
  37. }
  38. document.onmouseup = function (){
  39. _this.onup();
  40. }
  41. }
  42. Darg.prototype.onmove = function(ev){
  43. var oEvent = ev || event;
  44. var l = oEvent.clientX - this.disX;
  45. var t = oEvent.clientY - this.disY;
  46. this.oDiv.style.left = l + "px";
  47. this.oDiv.style.top = t + "px";
  48. document.title = l + " , " + t;
  49. return false;
  50. }
  51. Darg.prototype.onup = function(){
  52. document.onmousemove = null;
  53. document.onmouseup = null;
  54. }

  1. //面向对象拖拽-继承
  2. /*
  3. *CSS
  4. */
  5. #div1{ width:100px; height:100px; position:absolute; background:red; cursor:move;}
  6. #div2{ width:100px; height:100px; position:absolute; background:yellow; cursor:pointer;}
  7.  
  8. /*
  9. *HTML
  10. */
  11. <div id="div1"></div>
  12. <div id="div2"></div>
  13.  
  14. /*
  15. *Javascript
  16. */
  17. window.onload = function (){
  18.  
  19. new Darg("div1");
  20. new extendsDarg("div2");
  21.  
  22. }
  23. //面向对象拖拽开始
  24. function Darg(id){
  25. var _this = this;
  26. this.oDiv = document.getElementById(id);
  27. this.oDiv.onmousedown = function (ev){
  28. _this.ondown(ev);
  29. }
  30. }
  31. Darg.prototype.ondown = function(ev){
  32. var _this = this;
  33. var oEvent = ev || event;
  34. this.disX = oEvent.clientX - this.oDiv.offsetLeft;
  35. this.disY = oEvent.clientY - this.oDiv.offsetTop;
  36. document.onmousemove = function (ev){
  37. _this.onmove(ev);
  38. }
  39. document.onmouseup = function (){
  40. _this.onup();
  41. }
  42. }
  43. Darg.prototype.onmove = function(ev){
  44. var oEvent = ev || event;
  45. var l = oEvent.clientX - this.disX;
  46. var t = oEvent.clientY - this.disY;
  47. this.oDiv.style.left = l + "px";
  48. this.oDiv.style.top = t + "px";
  49. document.title = l + " , " + t;
  50. return false;
  51. }
  52. Darg.prototype.onup = function(){
  53. document.onmousemove = null;
  54. document.onmouseup = null;
  55. }
  56. //面向对象拖拽结束
  57.  
  58. //这里开始继承
  59. function extendsDarg(id){
  60. Darg.call(this,id);
  61. }
  62. for(i in Darg.prototype){
  63. extendsDarg.prototype[i] = Darg.prototype[i];
  64. }
  65. extendsDarg.prototype.onmove = function(ev){
  66. var oEvent = ev || event;
  67. var l = oEvent.clientX - this.disX;
  68. var t = oEvent.clientY - this.disY;
  69. if(l<0){
  70. l = 0;
  71. }else if(l>document.documentElement.clientWidth - this.oDiv.offsetWidth){
  72. l = document.documentElement.clientWidth - this.oDiv.offsetWidth;
  73. }
  74. if(t<0){
  75. t = 0;
  76. }else if(t>document.documentElement.clientHeight - this.oDiv.offsetHeight){
  77. t = document.documentElement.clientHeight - this.oDiv.offsetHeight;
  78. }
  79. this.oDiv.style.left = l + "px";
  80. this.oDiv.style.top = t + "px";
  81. document.title = l + " , " + t;
  82. return false;
  83. }

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. Abaqus中的单位制

    量纲 SI SI/mm US/ft US/inct 长度 m mm ft in 载荷 N N lbf lbf 质量 kg kg3 slug lbfs2/in 时间 s s s s 量纲 SI SI/m ...

  2. 【C语言】(数组方式)求n名同学的平均成绩

    原理就不说了 代码: #include <stdio.h> int main() { ], sum = ; int i; printf("请输入5名童鞋的成绩:\n") ...

  3. C语言:根据形参c中指定的英文字母,按顺序打印出若干后继相邻字母,-主函数中放入一个带头节点的链表结构中,h指向链表的头节点。fun函数找出学生的最高分-使用插入排序法对字符串中的字符进行升序排序。-从文件中找到指定学号的学生数据,读入次学生数据,

    //根据形参c中指定的英文字母,按顺序打印出若干后继相邻字母,输出字母的大小与形参c一致,数量由形参d指定.例如:输入c为Y,d为4,则输出ZABC. #include <stdio.h> ...

  4. Spring 事务管理的API

    Spring事务管理有3个API,均为接口. (1)PlatformTransactionManager    平台事务管理器 常用的实现类: DataSourceTransactionManager ...

  5. JavaScript的技巧45招

    JavaScript奇技淫巧45招 来自仲老师的分享: 原文地址[http://chensd.com/2015-01/45-useful-javascript-tips-tricks-and-best ...

  6. C:char类型

    char字符类型 字符型变量用于存储一个单一字符,在 C 语言中用 char 表示,其中每个字符变量都会占用 1 个字节.在给字符型变量赋值时,需要用一对英文半角格式的单引号(' ')把字符括起来. ...

  7. C的精神

    信任程序员 不要妨碍程序员做需要做的事 保持语言精练简单 只提供一种方法执行一项操作 让程序运行更快, 即使不能保证其可移植性 在最后一点上, 标准委员会的用意是: 作为实现, 应该针对目标计算机来定 ...

  8. hadoop集群启动报错: java.io.IOException: Incompatible clusterIDs

    java.io.IOException: Incompatible clusterIDs in /export/hadoop-2.7.5/hadoopDatas/datanodeDatas2: nam ...

  9. Spring Boot 2.x 入门前的准备-安装 Java JDK

    本章节介绍在以 window7.window10 为代表的 window 和 mac book 下安装 Java 编译和开发环境JDK 1.8,在 window 上安装 Java JDK 的步骤,本章 ...

  10. idea使用vue项目

    https://blog.csdn.net/qq_42564846/article/details/82688266