js组件化 2 拖拽
2016-07-19 11:20
281 查看
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>test</title> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <style> #div1{ width: 100px; height: 100px; background: red; cursor: move; position:absolute; left:0; top: 0; } #div2{ width: 100px; height: 100px; background: black; cursor: move; position:absolute; left:100px; top: 0; } #div3{ width: 100px; height: 100px; background: blue; cursor: move; position:absolute; left:200px; top: 0; } </style> <script> window.onload=function(){ var oDiv1=new Drag(); oDiv1.init({ id:'div1'}); var oDiv2=new Drag(); oDiv2.init({ id:'div2',fD:function(){ document.title="hi" }}); var oDiv3=new Drag(); oDiv3.init({ id:'div3', fD:function(){ document.title='jerry' }, fU:function(){ document.title='byebye' } }); } function Drag(){ this.oDiv=null;this.disX=0;this.disY=0;this.settings={fD:function(){},fU:function(){} }}; Drag.prototype.init=function(opt){ var _this=this; extend(this.settings,opt); this.oDiv = document.getElementById(opt.id); this.oDiv.onmousedown=function(ev){ var ev = ev||window.event; _this.fnDown(ev); _this.settings.fD(); document.onmousemove = function(ev){var ev=ev||window.event;_this.fnMove(ev);}; document.onmouseup = function(){_this.fnUp();_this.settings.fU();}; return false; } }; Drag.prototype.fnDown=function (ev){var ev=ev||window.event;this.disX=ev.clientX-this.oDiv.offsetLeft;this.disY=ev.clientY-this.oDiv.offsetTop;}; Drag.prototype.fnMove = function(ev){this.oDiv.style.left=ev.clientX-this.disX+'px';this.oDiv.style.top=ev.clientY-this.disY+'px';}; Drag.prototype.fnUp = function(){document.onmousedown=null;document.onmousemove=null;} function extend(obj1,obj2){for (var i in obj2){ obj1[i]=obj2[i];}} </script> </head> <body> <div id="div1"></div> <div id="div2"></div> <div id="div3"></div> </body> </html>
相关文章推荐
- JavaScript强化教程——DOM编程(两种控制div移动的方法)
- js函数必须在alert执行才能执行
- js实现动态创建的元素绑定事件
- 来来来,一起五句话搞定JavaScript作用域
- javascript中的定时方法
- a 中调用js的几种方法整理及使用推荐
- JavaScript强化教程——DOM编程(两种控制div移动的方法)
- js实现鼠标滚轮控制图片缩放效果的方法(转)
- json性能测试
- js本地存储 localstorage
- 用js动态添加html元素,以及属性的简单实例
- jstl 中function的使用
- 详谈JS中实现种子随机数及作用
- Ajax在静态页面中向指定url发送json请求获取返回的json数据
- js 屏蔽 浏览器(IE和FireFox)的 刷新功能
- 全面了解JavaScript对象进阶
- JS操作JSON总结
- javascript的日期对象Date操作时间日期值
- javascript花式理解闭包
- Angular.js学习之一下载和搭建hello world实例