JS拖拽-面向对象拖拽-继承
- //普通拖拽
- /*
- * 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拖拽-面向对象拖拽-继承的更多相关文章
- F2工作流引擎之-纯JS Web在线可拖拽的流程设计器(八)
Web纯JS流程设计器无需编程,完全是通过鼠标拖.拉.拽的方式来完成,支持串行.并行.分支.异或分支.M取N路分支.会签.聚合.多重聚合.退回.传阅.转交,都可以非常方便快捷地实现,管理员 ...
- js div浮动层拖拽效果代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 纯JS Web在线可拖拽的流程设计器
F2工作流引擎之-纯JS Web在线可拖拽的流程设计器 Web纯JS流程设计器无需编程,完全是通过鼠标拖.拉.拽的方式来完成,支持串行.并行.分支.异或分支.M取N路分支.会签.聚合.多重聚合.退回. ...
- js 实现table表格拖拽和点击表头升降序排序
js 实现table表格拖拽和点击表头升降序排序,写的比较乱,用的时候可以把其中的一些模块函数提取出来 样式,由于是可拖拽表格,所以样式 table tr th{cursor:move;} js实现 ...
- c# 实现文件拖入和拖出(拖拽)
摘自:http://www.cnblogs.com/eaglet/archive/2009/01/06/1370149.html C# WinForm下一步一步实现文件的拖入和拖出 作者:Eaglet ...
- 前端 ---JS中的面向对象
JS中的面向对象 创建对象的几种常用方式 1.使用Object或对象字面量创建对象 2.工厂模式创建对象 3.构造函数模式创建对象 4.原型模式创建对象 1.使用Object或对象字面量创建对象 ...
- python 全栈开发,Day52(关于DOM操作的相关案例,JS中的面向对象,定时器,BOM,client、offset、scroll系列)
昨日作业讲解: 京东购物车 京东购物车效果: 实现原理: 用2个盒子,就可以完整效果. 先让上面的小盒子向下移动1px,此时就出现了压盖效果.小盒子设置z-index压盖大盒子,将小盒子的下边框去掉, ...
- 前端JavaScript(3)-关于DOM操作的相关案例,JS中的面向对象、定时器、BOM、位置信息
小例子: 京东购物车 京东购物车效果: 实现原理: 用2个盒子,就可以完整效果. 先让上面的小盒子向下移动1px,此时就出现了压盖效果.小盒子设置z-index压盖大盒子,将小盒子的下边框去掉,就可以 ...
- 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.单继 ...
随机推荐
- 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 ...
- 【C语言】(数组方式)求n名同学的平均成绩
原理就不说了 代码: #include <stdio.h> int main() { ], sum = ; int i; printf("请输入5名童鞋的成绩:\n") ...
- C语言:根据形参c中指定的英文字母,按顺序打印出若干后继相邻字母,-主函数中放入一个带头节点的链表结构中,h指向链表的头节点。fun函数找出学生的最高分-使用插入排序法对字符串中的字符进行升序排序。-从文件中找到指定学号的学生数据,读入次学生数据,
//根据形参c中指定的英文字母,按顺序打印出若干后继相邻字母,输出字母的大小与形参c一致,数量由形参d指定.例如:输入c为Y,d为4,则输出ZABC. #include <stdio.h> ...
- Spring 事务管理的API
Spring事务管理有3个API,均为接口. (1)PlatformTransactionManager 平台事务管理器 常用的实现类: DataSourceTransactionManager ...
- JavaScript的技巧45招
JavaScript奇技淫巧45招 来自仲老师的分享: 原文地址[http://chensd.com/2015-01/45-useful-javascript-tips-tricks-and-best ...
- C:char类型
char字符类型 字符型变量用于存储一个单一字符,在 C 语言中用 char 表示,其中每个字符变量都会占用 1 个字节.在给字符型变量赋值时,需要用一对英文半角格式的单引号(' ')把字符括起来. ...
- C的精神
信任程序员 不要妨碍程序员做需要做的事 保持语言精练简单 只提供一种方法执行一项操作 让程序运行更快, 即使不能保证其可移植性 在最后一点上, 标准委员会的用意是: 作为实现, 应该针对目标计算机来定 ...
- hadoop集群启动报错: java.io.IOException: Incompatible clusterIDs
java.io.IOException: Incompatible clusterIDs in /export/hadoop-2.7.5/hadoopDatas/datanodeDatas2: nam ...
- Spring Boot 2.x 入门前的准备-安装 Java JDK
本章节介绍在以 window7.window10 为代表的 window 和 mac book 下安装 Java 编译和开发环境JDK 1.8,在 window 上安装 Java JDK 的步骤,本章 ...
- idea使用vue项目
https://blog.csdn.net/qq_42564846/article/details/82688266