JS 简单的拖拽移动效果
2017-01-04 14:14
429 查看
JS部分:
//html 元素 拖拽效果js类 --start--
function Drag()
{
//初始化
this.initialize.apply(this, arguments)
}
Drag.prototype = {
//初始化
initialize : function (drag, options)
{
this.drag = this.$(drag);
this._x = this._y = 0;
this._moveDrag = this.bind(this, this.moveDrag);
this._stopDrag = this.bind(this, this.stopDrag);
this.setOptions(options);
this.handle = this.$(this.options.handle);
this.maxContainer = this.$(this.options.maxContainer);
this.maxTop = Math.max(this.maxContainer.clientHeight, this.maxContainer.scrollHeight) - this.drag.offsetHeight;
this.maxLeft = Math.max(this.maxContainer.clientWidth, this.maxContainer.scrollWidth) - this.drag.offsetWidth;
this.limit = this.options.limit;
this.lockX = this.options.lockX;
this.lockY = this.options.lockY;
this.lock = this.options.lock;
this.onStart = this.options.onStart;
this.onMove = this.options.onMove;
this.onStop = this.options.onStop;
//this.handle.style.cursor = "move";
this.changeLayout();
this.addHandler(this.handle, "mousedown", this.bind(this, this.startDrag))
},
changeLayout : function ()
{
this.drag.style.top = this.drag.offsetTop + "px";
this.drag.style.left = this.drag.offsetLeft + "px";
this.drag.style.position = "absolute";
this.drag.style.margin = "0"
},
startDrag : function (event)
{
var event = event || window.event;
this._x = event.clientX - this.drag.offsetLeft;
this._y = event.clientY - this.drag.offsetTop;
this.addHandler(document, "mousemove", this._moveDrag);
this.addHandler(document, "mouseup", this._stopDrag);
event.preventDefault && event.preventDefault();
this.handle.setCapture && this.handle.setCapture();
this.onStart()
},
moveDrag : function (event)
{
var event = event || window.event;
var iTop = event.clientY - this._y;
var iLeft = event.clientX - this._x;
if (this.lock) return;
this.limit && (iTop < 0 && (iTop = 0), iLeft < 0 && (iLeft = 0), iTop > this.maxTop && (iTop = this.maxTop), iLeft > this.maxLeft && (iLeft = this.maxLeft));
this.lockY || (this.drag.style.top = iTop + "px");
this.lockX || (this.drag.style.left = iLeft + "px");
event.preventDefault && event.preventDefault();
this.onMove()
},
stopDrag : function ()
{
this.removeHandler(document, "mousemove", this._moveDrag);
this.removeHandler(document, "mouseup", this._stopDrag);
this.handle.releaseCapture && this.handle.releaseCapture();
this.onStop()
},
//参数设置
setOptions : function (options)
{
this.options =
{
handle: this.drag, //事件对象
limit: true, //锁定范围
lock: false, //锁定位置
lockX: false, //锁定水平位置
lockY: false, //锁定垂直位置
maxContainer: document.documentElement || document.body, //指定限制容器
onStart: function () {}, //开始时回调函数
onMove: function () {}, //拖拽时回调函数
onStop: function () {} //停止时回调函数
};
for (var p in options) this.options[p] = options[p]
},
//获取id
$ : function (id)
{
return typeof id === "string" ? document.getElementById(id) : id
},
//添加绑定事件
addHandler : function (oElement, sEventType, fnHandler)
{
return oElement.addEventListener ? oElement.addEventListener(sEventType, fnHandler, false) : oElement.attachEvent("on" + sEventType, fnHandler)
},
//删除绑定事件
removeHandler : function (oElement, sEventType, fnHandler)
{
return oElement.removeEventListener ? oElement.removeEventListener(sEventType, fnHandler, false) : oElement.detachEvent("on" + sEventType, fnHandler)
},
//绑定事件到对象
bind : function (object, fnHandler)
{
return function ()
{
return fnHandler.apply(object, arguments)
}
}
};
//html 元素 拖拽效果js类 --end--
//拖拽效果
//应用
window.onload = function ()
{
var oBox = document.getElementById("termMoveBox");
var oTitle = document.getElementById("tContainer");
var oDrag = new Drag(oBox, {handle:oTitle, limit:false});
//开始拖拽时方法
oDrag.onMove = function ()
{
oSpan.innerHTML = "left:" + this.drag.offsetLeft + ", top:" + this.drag.offsetTop
};
};
//拖拽end
HEML部分:
<style type="text/css">
.moveTitle{
font-family: "Arial Negreta","Arial Normal","Arial";
font-size: 14px;
font-style: normal;
font-weight: 700;
text-align: center;
line-height: 30px;
}
.moveBox{
width: 700px;
height: 450px;
background: #fff;
border: 1px solid green;
z-index: 999;
position: fixed;
left: 810px;
top: 155px;
overflow: hidden;
}
.moveBoxHeader{
background: #ccc none repeat scroll 0 0;
width: 100%;
height: 30px;
border-bottom: 1px solid #FFFFFF;
}
.moveBoxHeader p{
line-height: 20px;
background-color: #E8F1FB;
width: 80px;
text-align:center;
border-radius: 3px;
font-weight: normal;
cursor: pointer;
}
#tContainer{
min-width: 700px;
}
.scrollBox{
overflow-y:scroll;
width: 699px;
height: 386px;
}
</style>
<div id="termMoveBox" class="moveBox none">
<form id="tContainer">
<div class="moveBoxHeader">
<table style="width:100%;height:100%;">
<tr>
<th class="moveTitle" style="width:110px;">{"已选中列表"|L}</th>
<th class="moveTitle" id="termClean" style="width:auto;text-align:left;"><p>{"清空选中"|L}</p></th>
<th class="moveTitle" style="width:50px;color:blue;">
<p id="termCheckClose" style="margin-left:20px;width:20px;font-weight:bold;">x</p>
</th>
</tr>
</table>
</div>
<table class="base full">
<tr class='head'>
<th style="width:9px;"></th>
<th style="width:108px;text-align:left;">{"状态"|L}</th>
<th id="tgImeiIccid" style="width:152px;text-align:left;">IMEI</th>
<th id="tgMeidNumber" style="width:152px;text-align:left;">MEID</th>
<th style="text-align:left;">{"系统号码"|L}</th>
<th style="text-align:left;">{"操作"|L}</th>
</tr>
</table>
</form>
<div class="scrollBox">
<table class="base full" id="termMoveTable">
{*<tr>
<td style="width:4px;"></td>
<td style="width:103px;text-align:left;">开启</td>
<td style="width:147px;text-align:left;">122112555655555</td>
<td style="width:149px;text-align:left;">45612347894561</td>
<td style="text-align:left;">8sd12334566</td>
<td style="text-align:left;"><span style="color:#317EF3;cursor:pointer;" onclick="delTmove(this)">{"删除"|L}</span></td>
</tr>*}
</table>
</div>
</div>
//html 元素 拖拽效果js类 --start--
function Drag()
{
//初始化
this.initialize.apply(this, arguments)
}
Drag.prototype = {
//初始化
initialize : function (drag, options)
{
this.drag = this.$(drag);
this._x = this._y = 0;
this._moveDrag = this.bind(this, this.moveDrag);
this._stopDrag = this.bind(this, this.stopDrag);
this.setOptions(options);
this.handle = this.$(this.options.handle);
this.maxContainer = this.$(this.options.maxContainer);
this.maxTop = Math.max(this.maxContainer.clientHeight, this.maxContainer.scrollHeight) - this.drag.offsetHeight;
this.maxLeft = Math.max(this.maxContainer.clientWidth, this.maxContainer.scrollWidth) - this.drag.offsetWidth;
this.limit = this.options.limit;
this.lockX = this.options.lockX;
this.lockY = this.options.lockY;
this.lock = this.options.lock;
this.onStart = this.options.onStart;
this.onMove = this.options.onMove;
this.onStop = this.options.onStop;
//this.handle.style.cursor = "move";
this.changeLayout();
this.addHandler(this.handle, "mousedown", this.bind(this, this.startDrag))
},
changeLayout : function ()
{
this.drag.style.top = this.drag.offsetTop + "px";
this.drag.style.left = this.drag.offsetLeft + "px";
this.drag.style.position = "absolute";
this.drag.style.margin = "0"
},
startDrag : function (event)
{
var event = event || window.event;
this._x = event.clientX - this.drag.offsetLeft;
this._y = event.clientY - this.drag.offsetTop;
this.addHandler(document, "mousemove", this._moveDrag);
this.addHandler(document, "mouseup", this._stopDrag);
event.preventDefault && event.preventDefault();
this.handle.setCapture && this.handle.setCapture();
this.onStart()
},
moveDrag : function (event)
{
var event = event || window.event;
var iTop = event.clientY - this._y;
var iLeft = event.clientX - this._x;
if (this.lock) return;
this.limit && (iTop < 0 && (iTop = 0), iLeft < 0 && (iLeft = 0), iTop > this.maxTop && (iTop = this.maxTop), iLeft > this.maxLeft && (iLeft = this.maxLeft));
this.lockY || (this.drag.style.top = iTop + "px");
this.lockX || (this.drag.style.left = iLeft + "px");
event.preventDefault && event.preventDefault();
this.onMove()
},
stopDrag : function ()
{
this.removeHandler(document, "mousemove", this._moveDrag);
this.removeHandler(document, "mouseup", this._stopDrag);
this.handle.releaseCapture && this.handle.releaseCapture();
this.onStop()
},
//参数设置
setOptions : function (options)
{
this.options =
{
handle: this.drag, //事件对象
limit: true, //锁定范围
lock: false, //锁定位置
lockX: false, //锁定水平位置
lockY: false, //锁定垂直位置
maxContainer: document.documentElement || document.body, //指定限制容器
onStart: function () {}, //开始时回调函数
onMove: function () {}, //拖拽时回调函数
onStop: function () {} //停止时回调函数
};
for (var p in options) this.options[p] = options[p]
},
//获取id
$ : function (id)
{
return typeof id === "string" ? document.getElementById(id) : id
},
//添加绑定事件
addHandler : function (oElement, sEventType, fnHandler)
{
return oElement.addEventListener ? oElement.addEventListener(sEventType, fnHandler, false) : oElement.attachEvent("on" + sEventType, fnHandler)
},
//删除绑定事件
removeHandler : function (oElement, sEventType, fnHandler)
{
return oElement.removeEventListener ? oElement.removeEventListener(sEventType, fnHandler, false) : oElement.detachEvent("on" + sEventType, fnHandler)
},
//绑定事件到对象
bind : function (object, fnHandler)
{
return function ()
{
return fnHandler.apply(object, arguments)
}
}
};
//html 元素 拖拽效果js类 --end--
//拖拽效果
//应用
window.onload = function ()
{
var oBox = document.getElementById("termMoveBox");
var oTitle = document.getElementById("tContainer");
var oDrag = new Drag(oBox, {handle:oTitle, limit:false});
//开始拖拽时方法
oDrag.onMove = function ()
{
oSpan.innerHTML = "left:" + this.drag.offsetLeft + ", top:" + this.drag.offsetTop
};
};
//拖拽end
HEML部分:
<style type="text/css">
.moveTitle{
font-family: "Arial Negreta","Arial Normal","Arial";
font-size: 14px;
font-style: normal;
font-weight: 700;
text-align: center;
line-height: 30px;
}
.moveBox{
width: 700px;
height: 450px;
background: #fff;
border: 1px solid green;
z-index: 999;
position: fixed;
left: 810px;
top: 155px;
overflow: hidden;
}
.moveBoxHeader{
background: #ccc none repeat scroll 0 0;
width: 100%;
height: 30px;
border-bottom: 1px solid #FFFFFF;
}
.moveBoxHeader p{
line-height: 20px;
background-color: #E8F1FB;
width: 80px;
text-align:center;
border-radius: 3px;
font-weight: normal;
cursor: pointer;
}
#tContainer{
min-width: 700px;
}
.scrollBox{
overflow-y:scroll;
width: 699px;
height: 386px;
}
</style>
<div id="termMoveBox" class="moveBox none">
<form id="tContainer">
<div class="moveBoxHeader">
<table style="width:100%;height:100%;">
<tr>
<th class="moveTitle" style="width:110px;">{"已选中列表"|L}</th>
<th class="moveTitle" id="termClean" style="width:auto;text-align:left;"><p>{"清空选中"|L}</p></th>
<th class="moveTitle" style="width:50px;color:blue;">
<p id="termCheckClose" style="margin-left:20px;width:20px;font-weight:bold;">x</p>
</th>
</tr>
</table>
</div>
<table class="base full">
<tr class='head'>
<th style="width:9px;"></th>
<th style="width:108px;text-align:left;">{"状态"|L}</th>
<th id="tgImeiIccid" style="width:152px;text-align:left;">IMEI</th>
<th id="tgMeidNumber" style="width:152px;text-align:left;">MEID</th>
<th style="text-align:left;">{"系统号码"|L}</th>
<th style="text-align:left;">{"操作"|L}</th>
</tr>
</table>
</form>
<div class="scrollBox">
<table class="base full" id="termMoveTable">
{*<tr>
<td style="width:4px;"></td>
<td style="width:103px;text-align:left;">开启</td>
<td style="width:147px;text-align:left;">122112555655555</td>
<td style="width:149px;text-align:left;">45612347894561</td>
<td style="text-align:left;">8sd12334566</td>
<td style="text-align:left;"><span style="color:#317EF3;cursor:pointer;" onclick="delTmove(this)">{"删除"|L}</span></td>
</tr>*}
</table>
</div>
</div>
相关文章推荐
- 使用js实现的简单拖拽效果
- JS HTML5实现拖拽移动列表效果
- js学习一:简单的鼠标移动和移出效果案例
- js实现简单鼠标拖拽,文件拖入文件夹效果
- 手机端简单js拖拽效果
- 简单JS动画实例 广告移动效果
- JS实现简单拖拽效果
- JS简单拖拽效果
- js最简单的拖拽效果实现代码
- Android最简单的实现View拖拽跟随手指移动效果
- 简单实现js拖拽效果
- 一个用在手机上的简单js拖拽效果
- js 实现简单的动画效果(小圆移动)
- 简单实现谷歌地图的图片缩放,移动效果的JS
- js最简单的拖拽效果实现代码
- JS简单的块状效果
- JS网页图片查看器-可控制图片放大缩小还原移动效果
- 百度空间中实现拖拽、悬浮窗口效果的popup.js分析
- 简单实用的js缓冲效果
- Google 拖拽效果的JS完整注释