首页| 新闻| 娱乐| 游戏| 科普| 文学| 编程| 系统| 数据库| 建站| 学院| 产品| 网管| 维修| 办公| 热点
本文实例讲述了JavaScript使用面向对象实现的拖拽功能。分享给大家供大家参考,具体如下:
面向对象有个前提:
<!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>
希望本文所述对大家JavaScript程序设计有所帮助。
Intel工程样品CPU的识别方法
图解CMOS路线和硬盘光驱跳线的
硬盘分区如何设置准确的分区空间
回眸一笑百魅生,六宫粉黛无颜色
岁月静美,剪一影烟雨江南
芜湖有个“松鼠小镇”
小满:小得盈满,一切刚刚好!
一串串晶莹剔透的葡萄,像一颗颗宝石挂在藤
正宗老北京脆皮烤鸭
人逢知己千杯少,喝酒搞笑图集
搞笑试卷,学生恶搞答题
新闻热点
疑难解答
图片精选
《html》Js基础知识
js进行字符串模式匹配
Js返回值问题
Js操作BOM对象模型
网友关注