每日一个js实例8--通过面向对象实现拖拽
来源:互联网 发布:上海博科资讯java待遇 编辑:程序博客网 时间:2024/06/05 09:13
<div id="div2"></div>
#div1,#div2{background: red;width: 300px;height: 300px;position: absolute;}
<script>
//第一种写法将属性包装入构造函数中
// function Drag(id){
// this.oDiv=document.getElementById(id);
// var _this=this;
// this.x=0;
// this.y=0;
// this.oDiv.onmousedown=function(e){
// var e=e||event;
// _this.x=e.pageX-_this.oDiv.offsetLeft;
// _this.y=e.pageY-_this.oDiv.offsetTop;
// //alert(_this.x)
// document.onmousemove=function(e){
// var e=e||event;
// _this.oDiv.style.left=e.pageX-_this.x+"px";
// _this.oDiv.style.top=e.pageY-_this.y+"px";
// //alert(_this.oDiv.style.left)
// }
// document.onmouseup=function(e){
// var e=e||event;
// document.onmousemove=null;
// document.onmouseup=null;
// }
// return false;
// }
// }
// var div1=new Drag("div1")
// var div2=new Drag("div2")
//第二种方法,属性在构造函数中申明,方法在原型对象中申明
// function Drag(id){
// this.oDiv=document.getElementById(id);
// this.x=0;
// this.y=0;
// }
// Drag.prototype.draw=function(){
// var _this=this;
// this.oDiv.onmousedown=function(e){
// var e=e||event;
// _this.x=e.pageX-_this.oDiv.offsetLeft;
// _this.y=e.pageY-_this.oDiv.offsetTop;
// document.onmousemove=function(e){
// var e=e||event;
// _this.oDiv.style.left=e.pageX-_this.x+"px";
// _this.oDiv.style.top=e.pageY-_this.y+"px";
// }
// document.onmouseup=function(e){
// var e=e||event;
// document.onmousemove=null;
// document.onmouseup=null;
// }
// }
// }
// var div1=new Drag("div1")
// div1.draw();
// var div2=new Drag("div2")
// div2.draw();
//第三种写法,属性与方法的一部分在构造函数中申明,方法另一部分在原型对象中申明
// function Drag(id){
// this.oDiv=document.getElementById(id);
// var _this=this;
// this.x=0;
// this.y=0;
// this.oDiv.onmousedown=function(e){
// _this.draw(e);
// return false;
// }
// }
// Drag.prototype.draw=function(e){
// var e=e||event;
// var _this=this;
// this.x=e.pageX-this.oDiv.offsetLeft;
// this.y=e.pageY-this.oDiv.offsetTop;
// document.onmousemove=function(e){
// var e=e||event;
// _this.oDiv.style.left=e.pageX-_this.x+"px";
// _this.oDiv.style.top=e.pageY-_this.y+"px";
// }
// document.onmouseup=function(e){
// var e=e||event;
// document.onmousemove=null;
// document.onmouseup=null;
// }
// }
// var div1=new Drag("div1")
// var div2=new Drag("div2")
//第四种写法将属性方法全包装入构造函数中
function Drag(id){
this.oDiv=document.getElementById(id);
var _this=this;
this.x=0;
this.y=0;
this.draw=function(){
this.oDiv.onmousedown=function(e){
var e=e||event;
_this.x=e.pageX-_this.oDiv.offsetLeft;
_this.y=e.pageY-_this.oDiv.offsetTop;
//alert(_this.x)
document.onmousemove=function(e){
var e=e||event;
_this.oDiv.style.left=e.pageX-_this.x+"px";
_this.oDiv.style.top=e.pageY-_this.y+"px";
//alert(_this.oDiv.style.left)
}
document.onmouseup=function(e){
var e=e||event;
document.onmousemove=null;
document.onmouseup=null;
}
return false;
}
}
}
var div1=new Drag("div1")
div1.draw();
var div2=new Drag("div2")
div2.draw();
- 每日一个js实例8--通过面向对象实现拖拽
- 每日一个js实例5--通过面向对象实现运动
- 每日一个js实例6--通过面向对象实现进度条
- 每日一个js实例7--通过面向对象实现选项卡
- 每日一个js实例13--通过曲线反应json数据
- 每日一个js实例2--js与jq实现走马灯
- js面向对象实例(实现简易版拖拽)
- js面向对象实现元素拖拽
- js 面向对象编程实例
- 每日一个js实例9--NaN
- 每日一个js实例10--回车键
- 每日一个js实例14--随机数
- 每日一个js实例1---纯js与canvas实现心电图网格绘制
- js拖拽面向对象
- js开发:通过面向对象方式完成的拖拽功能
- JS面向对象轮播图实现
- js 实现面向对象编程
- 分享一个通过面向对象方式完成的拖拽功能
- manyToMany,设置了“CascadeType.PERSIST”,无法级联存储
- 如何理解HTML结构的语意化
- maven配置其他源下载jar包(详细)
- Glide的使用
- eclipse下生成Java类图和时序图,生成UML图
- 每日一个js实例8--通过面向对象实现拖拽
- QT - qss(二)组合框QComboBox的定制
- SharedUserId 简单 实现两个程序间的通信
- 如何实现字符串的反转及替换?
- javascript json对象操作(基本增删改查)
- C++ vector
- YII2框架详解
- Java8新特性二--Stream
- RBAC权限控制