拖拽系列二、利用JS面向对象OOP思想实现拖拽封装

时间:2023-03-08 20:06:39

接着上一篇拖拽系列一、JavaScript实现简单的拖拽效果这一篇博客将接着对上一节实现代码利用JS面向对象(OOP)思维对上一节代码进行封装;

使其模块化、避免全局函数污染、方便后期维护和调用;写到这里突然想起一句话“没有任何一个题目是彻底完成的。总还会有很多事情可做......”

拖拽系列二、利用JS面向对象OOP思想实现拖拽封装

我想这句话程序开发大概也适用吧,前端开发人员总是可以结合自己之前学到“拖拽”相关知识,不断扩展、完善、无穷无尽.......

    利用匿名函数自执行实现封装

;(function(){
//do something......
})();

这样写的好处是避免全局变量、全局函数的污染;原因是函数存在作用域、作用域链、执行上下文;分号作用是避免代码压缩出错问题

    面向对象(OOP)及代码大致结构简述

1.构造函数主要用于构造实例化的对象,每个对象都有自己特有(私有)的属性和方法(该属性和方法只供当前实例化对象调用);

2.对象原型通过调用构造函数实例化对象对属性与方法的实现共享

3.普通函数模块化

面向对象(OOP)思维分析拖拽

重复说明——理解拖拽的核心就是掌握鼠标相关事件及鼠标事件相关联的操作流程

被拖拽的目标元素对象有

①鼠标按下时mousedown;

初始化鼠标的位置initMouseX、initMouseY;初始化目标元素位置initObjX、initObjY;鼠标按下标识isDraging

②鼠标移动时mousemove; 获取鼠标移动时的位置、计算目标元素的移动距离、设置目标元素的距离

③鼠标离开时mouseup; 停止移动,移除目标元素事件绑定;

代码大致结构如下

 /*
* 利用OOP(面向对象) 实现拖拽代码的封装
*/
;(function(){
//事件处理程序
//elem DOM对象 eventName 事件名称 eventType 事件类型
function eventHandler(elem, eventName, eventType){};
//移除事件兼容处理
function removeEventHandler(elem, eventName, eventType){}
//获取style属性值
function getStyleValue(elem, property){}
//被拖拽构造函数
function Drag(selector){
//elem DOM对象
this.elem = typeof selector === 'object' ? selector : document.getElementById(selector);
//元素初始化位置
this.initObjX = 0;
this.initObjY = 0;
//鼠标初始化位置
this.initMouseX = 0;
this.initMouseY = 0;
this.isDraging = false;
//初始化--鼠标事件操作
this._init();
}
//Drag对象原型
Drag.prototype = {
constructor : Drag,
//初始化
//构造原型指回Drag 等价于==>>Drag.prototype._init = function(){}
_init : function(){
this.setDrag();
},
//获取目标元素pos位置
getObjPos : function(elem) {},
//设置被拖动元素的位置
setObjPos : function (elem, pos){},
//设置目标元素事件及操作流程
setDrag : function(){}
}
//将Drag挂到全局对象window上
window.Drag = Drag;
})();

    实现拖拽功能函数的详述

上述较复杂思路集中在构造函数Drag创建被拖拽目标元素实例化对象,初始化设置目标元素鼠标事件及操作流程上,细节代码如下

 //被拖拽构造函数
function Drag(selector){
//elem DOM对象
this.elem = typeof selector === 'object' ? selector : document.getElementById(selector);
//元素初始化位置
this.initObjX = 0;
this.initObjY = 0;
//鼠标初始化位置
this.initMouseX = 0;
this.initMouseY = 0;
this.isDraging = false;
//初始化--鼠标事件操作
this._init();
}
//Drag对象原型
Drag.prototype = {
constructor : Drag,
//初始化
//构造原型指回Drag 等价于==>>Drag.prototype._init = function(){}
_init : function(){
this.setDrag();
},
//设置目标元素事件及操作流程
setDrag : function(){
//目标元素对象
var self = this;
var time = null; //定时器
function mousedown(event){
event = window.event || event;
//鼠标按下时位置
this.initMouseX = event.clientX;
this.initMouseY = event.clientY;
//获取元素初始化位置pos
var pos = self.getObjPos(self.elem);
this.initObjX = pos.x;
this.initObjY = pos.y;
//mousemove
time = setTimeout(function(){ //缓解移动卡顿
eventHandler(self.elem, mousemove, "mousemove");
}, 25);
//mouseup
eventHandler(self.elem, mouseup, "mouseup");
//按下标识
self.isDraging = true;
}
function mousemove(event){
event = window.event || event;
if(self.isDraging){
//元素移动位置 == 当前鼠标移动位置 - 鼠标按下位置 + 目标元素初始化位置
var moveX = event.clientX - this.initMouseX + this.initObjX;
var moveY = event.clientY - this.initMouseY + this.initObjY;
//设置拖拽元素位置
self.setObjPos(self.elem, {
x : moveX,
y : moveY,
});
}
}
function mouseup(event){
event = window.event || event;
self.isDraging = false;
clearTimeout(time);
//移除事件
removeEventHandler(document, mousemove, 'mousemove');
removeEventHandler(document, mouseup, 'mouseup');
}
//mousedown
eventHandler(this.elem, mousedown, "mousedown");
}
}

至于设置/获取被拖拽的目标元素就很简单咯!

调用写法  new Drag(elem); elem为传入DOM对象即可

    JS面向对象OOP实现拖拽完整代码

HTML代码

 <style>
body {
margin: 0;
padding: 0;
position: relative;
}
.box {
width: 100px;
height: 100px;
background: deeppink;
position: absolute;
left: 25px;
top: 25px;
cursor: move;
}
</style>
<div class="box" id="box" style="position: absolute;left: 25px;top: 25px;"></div>
<script src="js/draging.js"></script>
<script type="text/javascript">
window.onload = function(){
new Drag(document.getElementById("box"));
}
</script>

draging.js

 /*
* 利用JS面向对象OOP思想实现拖拽封装
*/
;(function(){
//事件处理程序
//elem DOM对象 eventName 事件名称 eventType 事件类型
function eventHandler(elem, eventName, eventType){
// elem.attachEvent 兼容IE9以下事件
elem.addEventListener ? elem.addEventListener(eventType, eventName, false) : elem.attachEvent('on'+eventType, eventName);
};
//移除事件兼容处理
function removeEventHandler(elem, eventName, eventType){
elem.removeEventListener ? elem.removeEventListener(eventType, eventName) : elem.detachEvent(eventType, eventName);
}
//获取style属性值
function getStyleValue(elem, property){
//getComputedStyle、currentStyle 返回CSS样式声明对象([object CSSStyleDeclaration]) 只读
//getComputedStyle 支持IE9+以上及正常浏览器
//currentStyle 兼容IE8及IE8以下获取目标元素style样式
return window.getComputedStyle(elem,null) ? window.getComputedStyle(elem,null)[property] : elem.currentStyle[property];
}
//被拖拽构造函数
function Drag(selector){
//elem DOM对象
this.elem = typeof selector === 'object' ? selector : document.getElementById(selector);
//元素初始化位置
this.initObjX = 0;
this.initObjY = 0;
//鼠标初始化位置
this.initMouseX = 0;
this.initMouseY = 0;
this.isDraging = false;
//初始化--鼠标事件操作
this._init();
}
//Drag对象原型
Drag.prototype = {
constructor : Drag,
//初始化
//构造原型指回Drag 等价于==>>Drag.prototype._init = function(){}
//初始化鼠标事件及鼠标操作流程
_init : function(){
this.setDrag();
},
//获取目标元素pos位置
getObjPos : function(elem) {
var pos = {x: 0, y: 0};
if(getStyleValue(elem, 'position') == 'static') {
this.elem.style.position = 'relative';
return pos;
} else {
var x = parseInt(getStyleValue(elem, 'left') ? getStyleValue(elem, 'left') : 0);
var y = parseInt(getStyleValue(elem, 'top') ? getStyleValue(elem, 'top') : 0);
return pos = {
x: x,
y: y
}
}
},
//设置被拖动元素的位置
setObjPos : function (elem, pos){
elem.style.position = 'absolute';
elem.style.left = pos.x+'px';
elem.style.top = pos.y+'px';
},
//设置目标元素事件及操作流程
setDrag : function(){
//目标元素对象
var self = this;
var time = null; //定时器
function mousedown(event){
event = window.event || event;
//鼠标按下时位置
this.initMouseX = event.clientX;
this.initMouseY = event.clientY;
//获取元素初始化位置pos
var pos = self.getObjPos(self.elem);
this.initObjX = pos.x;
this.initObjY = pos.y;
//mousemove
time = setTimeout(function(){ //缓解移动卡顿
eventHandler(self.elem, mousemove, "mousemove");
}, 25);
//mouseup
eventHandler(self.elem, mouseup, "mouseup");
//按下标识
self.isDraging = true;
}
function mousemove(event){
event = window.event || event;
if(self.isDraging){
//元素移动位置 == 当前鼠标移动位置 - 鼠标按下位置 + 目标元素初始化位置
var moveX = event.clientX - this.initMouseX + this.initObjX;
var moveY = event.clientY - this.initMouseY + this.initObjY;
//设置拖拽元素位置
self.setObjPos(self.elem, {
x : moveX,
y : moveY,
});
}
}
function mouseup(event){
event = window.event || event;
self.isDraging = false;
clearTimeout(time);
//移除事件
removeEventHandler(document, mousemove, 'mousemove');
removeEventHandler(document, mouseup, 'mouseup');
}
//mousedown
eventHandler(this.elem, mousedown, "mousedown");
}
}
//将Drag挂到全局对象window上
window.Drag = Drag;
})();

在线编辑代码请点击 http://jsrun.net/uukKp/edit

作者:Avenstar

出处:http://www.cnblogs.com/zjf-1992/p/6854783.html

关于作者:专注于前端开发、喜欢阅读

本文版权归作者所有,转载请标明原文链接