JavaScript使用面向对象实现的拖拽功能详解
程序员文章站
2023-10-23 18:44:11
本文实例讲述了javascript使用面向对象实现的拖拽功能。分享给大家供大家参考,具体如下:
面向对象有个前提:
前提:所有东西都必须包含在onload里...
本文实例讲述了javascript使用面向对象实现的拖拽功能。分享给大家供大家参考,具体如下:
面向对象有个前提:
- 前提:所有东西都必须包含在onload里
- 改写:不能有函数嵌套,可以有全局变量
- 过程,如下
- onload改成构造函数,
- 全局变量改成属性(通过this)
- 函数改写成方法
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>面向对象的继承-1</title> <style> #div1 {width: 100px; height: 100px; background: red; position: absolute;} </style> <script> window.onload = function() { var odiv = document.getelementbyid('div1'); odiv.onmousedown = function(ev) { var ev = ev || event; var disx = ev.clientx - this.offsetleft; var disy = ev.clienty - this.offsettop; document.onmousemove = function(ev) { var ev = ev || event; odiv.style.left = ev.clientx - disx + 'px'; odiv.style.top = ev.clienty - disy + 'px'; } document.onmouseup = function() { document.onmousemove = document.onmouseup = null; } } } </script> </head> <body> <div id="div1"></div> </body> </html>
把局部变量改成全局变量
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>面向对象的继承-2</title> <style> #div1 {width: 100px; height: 100px; background: red; position: absolute;} </style> <script> var odiv=null; var disx=0; var disy=0; window.onload = function() { odiv = document.getelementbyid('div1'); odiv.onmousedown = fndown; } function fnmove(ev) { var ev = ev || event; odiv.style.left = ev.clientx - disx + 'px'; odiv.style.top = ev.clienty - disy + 'px'; } function fnup() { document.onmousemove = document.onmouseup = null; } function fndown(ev) { var ev = ev || event; disx = ev.clientx - this.offsetleft; disy = ev.clienty - this.offsettop; document.onmousemove = fnmove; document.onmouseup =fnup; } </script> </head> <body> <div id="div1"></div> </body> </html>
引用块内容
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>面向对象的继承-2</title> <style> #div1 {width: 100px; height: 100px; background: red; position: absolute;} #div2 {width: 100px; height: 100px; background: red; position: absolute;top:120px;} </style> <script> window.onload=function(){ new drag('div1'); new drag('div2'); } function drag(id) { var _this=this; this.disx=0; this.disy=0; this.odiv = document.getelementbyid(id); this.odiv.onmousedown = function(){ _this.fndown() }; } drag.prototype.fndown=function (ev) { var ev = ev || event; var _this=this; this.disx = ev.clientx - this.odiv.offsetleft; this.disy = ev.clienty - this.odiv.offsettop; document.onmousemove = function(){ _this.fnmove(); }; document.onmouseup =function(){ _this.fnup(); }; } drag.prototype.fnmove=function(ev) { var ev = ev || event; this.odiv.style.left = ev.clientx - this.disx + 'px'; this.odiv.style.top = ev.clienty - this.disy + 'px'; } drag.prototype.fnup=function () { document.onmousemove = null; document.onmouseup = null } </script> </head> <body> <div id="div1"></div> <div id="div2"></div> </body> </html>
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>面向对象的继承-2</title> <style> #div1 {width: 100px; height: 100px; background: red; position: absolute;} #div2 {width: 100px; height: 100px; background: red; position: absolute;top:120px;} </style> <script> window.onload=function(){ new drag('div1'); new drag('div2'); } function drag(id) { var _this=this; this.disx=0; this.disy=0; this.odiv = document.getelementbyid(id); this.odiv.onmousedown = function(){ _this.fndown() }; } drag.prototype.fndown=function (ev) { var ev = ev || event; var _this=this; this.disx = ev.clientx - this.odiv.offsetleft; this.disy = ev.clienty - this.odiv.offsettop; document.onmousemove = function(){ _this.fnmove(); }; document.onmouseup =function(){ _this.fnup(); }; } drag.prototype.fnmove=function(ev) { var ev = ev || event; this.odiv.style.left = ev.clientx - this.disx + 'px'; this.odiv.style.top = ev.clienty - this.disy + 'px'; } drag.prototype.fnup=function () { document.onmousemove = null; document.onmouseup = null } </script> </head> <body> <div id="div1"></div> <div id="div2"></div> </body> </html>
感兴趣的朋友可以使用在线html/css/javascript代码运行工具:http://tools.jb51.net/code/htmljsrun测试一下运行效果。
更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript页面元素操作技巧总结》、《javascript操作dom技巧总结》、《javascript切换特效与技巧总结》、《javascript动画特效与技巧汇总》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》及《javascript数学运算用法总结》
希望本文所述对大家javascript程序设计有所帮助。
上一篇: 古代皇帝明明可以随便找媳妇 为什么他们偏偏喜欢别人的老婆呢
下一篇: 八种不同类型人群的八种喝水方法
推荐阅读
-
Android编程实现全局获取Context及使用Intent传递对象的方法详解
-
javascript使用substring实现的展开与收缩文字功能示例
-
Android编程使用加速度传感器实现摇一摇功能及优化的方法详解
-
JavaScript使用面向对象实现的拖拽功能详解
-
JavaScript实现图片的放大缩小及拖拽功能示例
-
Python 使用 attrs 和 cattrs 实现面向对象编程的实践
-
JavaScript实现的XML与JSON互转功能详解
-
如何用JavaScript实现功能齐全的单链表详解
-
Android编程实现全局获取Context及使用Intent传递对象的方法详解
-
javascript使用substring实现的展开与收缩文字功能示例