完美的js div拖拽实例代码

所属分类: 网络编程 / JavaScript 阅读数: 696
收藏 0 赞 0 分享

本文实例为大家分享了完美的js div拖拽实例代码,供大家参考,具体内容如下

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>拖拽库</title>
<style type="text/css">
div,h2,p{margin:0;padding:0;}
body{font:14px/1.5 arial;}
#box{width:100px;height:100px;background:#fef4eb;padding:5px;margin:50px;border:1px solid #f60;}
#box .title{height:25px;background:#f60;}
#tool{margin-bottom:10px;}
</style>
<script type="text/javascript">
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) 
 }
 }
};
 
 
//应用
window.onload = function ()
{
 var oBox = document.getElementById("box"); 
 var oTitle = oBox.getElementsByTagName("h2")[0];
 var oSpan = document.getElementsByTagName("span")[0];
 var oDrag = new Drag(oBox, {handle:oTitle, limit:false});

 var aInput = document.getElementsByTagName("input");

 //锁定范围接口
 aInput[0].onclick = function ()
 {
 oDrag.limit = !oDrag.limit;
 this.value = oDrag.limit ? "取消锁定范围" : "锁定范围"
 };

 //水平锁定接口
 aInput[1].onclick = function ()
 {
 oDrag.lockX = !oDrag.lockX;
 this.value = oDrag.lockX ? "取消水平锁定" : "水平锁定"
 };

 //垂直锁定接口
 aInput[2].onclick = function ()
 {
 oDrag.lockY = !oDrag.lockY;
 this.value = oDrag.lockY ? "取消垂直锁定" : "垂直锁定"
 };

 //锁定位置接口
 aInput[3].onclick = function ()
 {
 oDrag.lock = !oDrag.lock;
 this.value = oDrag.lock ? "取消锁定位置" : "锁定位置"
 };

 //开始拖拽时方法
 oDrag.onStart = function ()
 {
 oSpan.innerHTML = "开始拖拽" 
 };

 //开始拖拽时方法
 oDrag.onMove = function ()
 {
 oSpan.innerHTML = "left:" + this.drag.offsetLeft + ", top:" + this.drag.offsetTop 
 };

 //开始拖拽时方法
 oDrag.onStop = function ()
 {
 oSpan.innerHTML = "结束拖拽" 
 };
};
</script>
</head>
<body>
<div id="tool">
 <input type="button" value="锁定范围" />
  <input type="button" value="水平锁定" />
  <input type="button" value="垂直锁定" />
  <input type="button" value="锁定位置" />
</div>
<p>拖放状态:<span>未开始</span></p>
<div id="box">
 <h2 class="title"></h2>
</div>
</body>
</html>
</td>
  </tr>
 </table>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Angular使用Md5加密的解决方法

这篇文章主要介绍了Angular使用Md5加密的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解JS构造函数中this和return

本文通过实例代码给大家介绍了JS构造函数中this和return,需要的朋友参考下吧
收藏 0 赞 0 分享

ES6中Array.find()和findIndex()函数的用法详解

ES6为Array增加了find(),findIndex函数。find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined,而findIndex()函数也是查找目标元素,找到就返回元素的位置,找不到就返回-1。下面通过实例详解,需要的朋友参考下吧
收藏 0 赞 0 分享

JS闭包的几种常见形式实例详解

本文通过实例代码给大家详细介绍了js闭包的几种常见形式,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下
收藏 0 赞 0 分享

ES6中Array.copyWithin()函数的用法实例详解

ES6为Array增加了copyWithin函数,用于操作当前数组自身,用来把某些个位置的元素复制并覆盖到其他位置上去。下面重点给大家介绍ES6中Array.copyWithin()函数的用法,需要的朋友参考下
收藏 0 赞 0 分享

Javascript 严格模式use strict详解

严格模式:由ECMA-262规范定义的JavaScript标准,对javascrip的限制更强。这篇文章主要介绍了Javascript 严格模式use strict详解 ,需要的朋友可以参考下
收藏 0 赞 0 分享

引入JavaScript时alert弹出框显示中文乱码问题

今天在HTML中引入JavaScript文件运行时,alert弹出的提示框中文显示为乱码,怎么解决此问题呢?下面小编给大家带来了引入JavaScript时alert弹出框显示中文乱码问题的解决方法,一起看看吧
收藏 0 赞 0 分享

AngularJs 延时器、计时器实例代码

这篇文章主要介绍了AngularJs 延时器、计时器实例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

JS分页的实现(同步与异步)

这篇文章主要介绍了JS分页的实现(同步与异步),需要的朋友可以参考下
收藏 0 赞 0 分享

Angularjs自定义指令实现分页插件(DEMO)

由于最近的一个项目使用的是angularjs1.0的版本,涉及到分页查询数据的功能,后来自己就用自定义指令实现了该功能,下面小编把实例demo分享到脚本之家平台,需要的朋友参考下
收藏 0 赞 0 分享
查看更多