JavaScript面向对象_拖拽
来源:互联网 发布:数据的存储过程 编辑:程序博客网 时间:2024/06/05 09:43
面向对象有个前提:
- 前提:所有东西都必须包含在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>
阅读全文
0 0
- JavaScript面向对象_拖拽
- JavaScript学习总结_六.面向对象
- 面向对象_静态
- 面向对象_封装
- 面向对象_继承
- 面向对象_多态
- 面向对象_继承
- 面向对象_总结
- 面向对象_封装
- 面向对象_静态
- javaScript 面向对象-对象
- 面向对象_匿名对象
- 韩顺平 javascript教学视频_学习笔记12_js面向对象编程介绍_类(原型对象)和对象
- javascript 面向对象初探
- JavaScript 面向对象编程
- JavaScript 面向对象编程
- 面向对象的javascript
- javascript面向对象编程
- java 操作 word 资料整理
- redis keys的通用操作
- 性能优化知多少
- 最终,还是没能成为吹牛逼的资本!(结束)
- Android JieCaoVideoPlayer 使用入门指南
- JavaScript面向对象_拖拽
- 从尾到头打印链表
- Available parameters are [arg1, arg0, param1, param2]
- 考研倒计时160天
- shiro权限管理入门
- synchronized详解
- secureCRT 配置端口转发用于 SFTP 文件传输
- 聊天界面视图显示最新消息(jq)
- php开发神器 -- phpStudy