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=utf-8" />
<title>js div浮动层拖拽效果代码 - 站长素材</title>
<style type="text/css">
div,h2,p{margin:0;padding:0;}
body{font:14px/1.5 arial;}
#box{width:400px;height:200px;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>
<br /><br /><br /><br /><br />
<center>
<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>
</center>
</body>
</html>
js div浮动层拖拽效果代码的更多相关文章
- 【j2ee】div浮动层拖拽
背景:近期项目中需要实现弹出浮层增加数据,并且浮动层可以拖拽 解决步骤:1.浮动层实现 2.拖拽实现 多方查资料,基本实现功能,现做demo,便于以后使用 先上图片大体展示实现效果: 再上代码,展示 ...
- 用JavaScript实现div的鼠标拖拽效果
实现原理鼠标按下时根据onmousemove事件来动态获取鼠标坐标位置以此来更新div的位置,实现的前提时div要有一个定位效果,不然的话是移动不了它的. HTML <div class=&qu ...
- 原生js实现模块来回拖拽效果
代码比较冗余,还没来得及做整理,往见谅. 主要用到的 JS 事件有: onmousedown:鼠标点下事件 onmousemove:鼠标移动事件 onmouseup:鼠标放开事件 具体代码如下: &l ...
- React.js实现原生js拖拽效果及思考
一.起因&思路 不知不觉,已经好几天没写博客了...近来除了研究React,还做了公司官网... 一直想写一个原生js拖拽效果,又加上近来学react学得比较嗨.所以就用react来实现这个拖 ...
- JS实现漂亮的窗口拖拽效果(可改变大小、最大化、最小化、关闭)
转自<JS实现漂亮的窗口拖拽效果(可改变大小.最大化.最小化.关闭)>:http://www.jb51.net/article/73157.htm 这篇文章主要介绍了JS实现漂亮的窗口 ...
- js实现本地图片文件拖拽效果
如何拖拽图片到指定位置,具体方法如下 在从本地上传图片的时候,如果使用拖拽效果,想想应该是更加的高大上,下面直接上js代码 完整代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- 原生js简单实现拖拽效果
实现弹窗拖拽效果的原理是:按下鼠标并移动——拖拽移动物体,抬起鼠标——停止移动.主要触发三个事件:onmousedown.onmousemove以及onmouseup: 首先搭建结构:一个宽350px ...
- js拖拽效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JS实现拖拽效果
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
随机推荐
- javascript从url中获取请求参数
function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)( ...
- JavaScript---Angular 和JQuery
Angular 三层模式 M model和data 数据层 , V view 视图层 , C controller 控制器,程序主逻辑 ,通过指令扩展HTML,通过表达式绑定数据到HTML. Vi ...
- java中的单引号和双引号
1.单引号引的数据 是char类型的,双引号引的数据 是String类型的:单引号只能引一个字符,而双引号可以引0个及其以上.char只是一个基本类型,而String 可以是一个类,可以直接引用.比如 ...
- scala学习之第二天:可变容器与不可变容器的特性与应用
1.具体的不可变集合实体类 List(列表) 是一种有限的不可变序列式.提供了常数时间的访问列表头元素和列表尾的操作,并且提供了常数时间的构造新链表的操作,该操作将一个新的元素插入到列表的头部.其他许 ...
- How to configure SRTM elevations in WorldWind WMS
In this thread I will try to explain how to serve SRTM elevations using NASA WorldWind WMS. ! Import ...
- http 请求头设置缓存
nginx不缓存设置 2013-08-15 10:47:39 分类: LINUX 在开发调试web的时候,经常会碰到因浏览器缓存(cache)而经常要去清空缓存或者强制刷新来测试的烦恼,提供下apa ...
- 一个Eclipse代码显示主题
- Activiti系列:为什么Activiti 5.18 的REST的api总是返回404错误
REST api可以访问了,如下 1.修改db.properties配置文件,让他访问sql server 2.在浏览器中输入如下地址,注意中间有一个service,这点和之前的不一样,在<Ac ...
- iOS项目重构日记
如何重构 首先,要对程序的一般架构烂熟于心,尤其是MVC,这是基本.还有就是分离存储和网络请求的逻辑. 对于一些常用的控件尽量分离复用,设置开关函数,适当的时候开启,不要的时候关闭,有必要的话还可以 ...
- UIPasteboard的使用
剪贴板的使用以及自定义剪贴板. 系统剪贴板的直接调用 其实整个过程非常的简单,我就用我写的一个自定义UILable来说明调用系统剪贴板. 首先,因为苹果只放出来了 UITextView,UITextF ...