js拖拽效果的实现
1、最基础的写法
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 {width:200px; height:200px; background:yellow; position:absolute;}
</style>
<script>
window.onload=function ()
{
var oDiv=document.getElementById('div1'); oDiv.onmousedown=function (ev)
{
var oEvent=ev||event; var disX=oEvent.clientX-oDiv.offsetLeft;
var disY=oEvent.clientY-oDiv.offsetTop; document.onmousemove=function (ev)
{
var oEvent=ev||event; oDiv.style.left=oEvent.clientX-disX+'px';
oDiv.style.top=oEvent.clientY-disY+'px';
}; document.onmouseup=function ()
{
document.onmousemove=null;
document.onmouseup=null;
};
};
};
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
2、相比较高级的写法
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 {width:200px; height:200px; background:yellow; position:absolute;}
</style>
<script>
var oDiv=null;
var disX=0;
var disY=0; window.onload=function ()
{
oDiv=document.getElementById('div1'); oDiv.onmousedown=fnDown;
}; function fnDown(ev)
{
var oEvent=ev||event; disX=oEvent.clientX-oDiv.offsetLeft;
disY=oEvent.clientY-oDiv.offsetTop; document.onmousemove=fnMove;
document.onmouseup=fnUp;
} function fnMove(ev)
{
var oEvent=ev||event; oDiv.style.left=oEvent.clientX-disX+'px';
oDiv.style.top=oEvent.clientY-disY+'px';
} function fnUp()
{
document.onmousemove=null;
document.onmouseup=null;
}
</script>
</head> <body>
<div id="div1"></div>
</body>
</html>
3、
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 {width:200px; height:200px; background:yellow; position:absolute;}
#div2 {width:200px; height:200px; background:green; position:absolute;}
</style>
<script src="Drag.js"></script>
<script src="LimitDrag.js"></script>
<script>
window.onload=function ()
{
new Drag('div1');
new LimitDrag('div2');
};
</script>
</head> <body>
<div id="div1">普通拖拽</div>
<div id="div2">限制范围</div>
</body>
</html>
Dray.js
function Drag(id)
{
var _this=this;
this.disX=0;
this.disY=0; this.oDiv=document.getElementById(id);
this.oDiv.onmousedown=function (ev)
{
_this.fnDown(ev); return false;
};
}; Drag.prototype.fnDown=function (ev)
{
var _this=this;
var oEvent=ev||event; this.disX=oEvent.clientX-this.oDiv.offsetLeft;
this.disY=oEvent.clientY-this.oDiv.offsetTop; document.onmousemove=function (ev)
{
_this.fnMove(ev);
};
document.onmouseup=function ()
{
_this.fnUp();
};
}; Drag.prototype.fnMove=function (ev)
{
var oEvent=ev||event; this.oDiv.style.left=oEvent.clientX-this.disX+'px';
this.oDiv.style.top=oEvent.clientY-this.disY+'px';
}; Drag.prototype.fnUp=function ()
{
document.onmousemove=null;
document.onmouseup=null;
};
LimitDrag.js
function LimitDrag(id)
{
Drag.call(this, id); //继承属性
} for(var i in Drag.prototype)
{
LimitDrag.prototype[i]=Drag.prototype[i];
} LimitDrag.prototype.fnMove=function (ev)
{
var oEvent=ev||event;
var l=oEvent.clientX-this.disX;
var t=oEvent.clientY-this.disY; if(l<)
{
l=0;
}
else if(l>document.documentElement.clientWidth-this.oDiv.offsetWidth)
{
l=document.documentElement.clientWidth-this.oDiv.offsetWidth;
} this.oDiv.style.left=l+'px';
this.oDiv.style.top=t+'px';
};
js拖拽效果的实现的更多相关文章
- React.js实现原生js拖拽效果及思考
一.起因&思路 不知不觉,已经好几天没写博客了...近来除了研究React,还做了公司官网... 一直想写一个原生js拖拽效果,又加上近来学react学得比较嗨.所以就用react来实现这个拖 ...
- js拖拽效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 再谈React.js实现原生js拖拽效果
前几天写的那个拖拽,自己留下的疑问...这次在热心博友的提示下又修正了一些小小的bug,也加了拖拽的边缘检测部分...就再聊聊拖拽吧 一.不要直接操作dom元素 react中使用了虚拟dom的概念,目 ...
- js拖拽效果的实现及原理
元素拖拽分成3个步骤:按下鼠标,移动鼠标,松开鼠标. 拖拽原理:按下拖拽元素后开始监听文档中鼠标移动事件,然后再监听鼠标松开事件:鼠标移动时,元素div要随着鼠标一起移动,需要计算元素div位移的距离 ...
- js拖拽效果实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js拖拽效果详细讲解
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js div浮动层拖拽效果代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 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 ...
随机推荐
- C#基础-数组-ArrayList
数组ArrayList using System.Collections; //表示引入集合的命名空间 数组ArrayList容量本身是不固定的,根据存储的数据动态变化 // 声明一个ArrayLis ...
- 收集的免费API接口
1.IP地址调用接口 这是淘宝的IP调用API http://ip.taobao.com/service/getIpInfo.php?ip=$ip 返回值:{"code":0,&q ...
- python日记整理
都是自己的学习总结,要是总结的有问题大佬麻烦评价一下我好修改,谢谢 python插件插件+pycharm基本用法+markdown文本编写+jupyter notebook的基本操作汇总 一.计算机基 ...
- leetcode-25-exercise_string&array
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- (转)iOS获取设备型号
//获得设备型号 + (NSString *)getCurrentDeviceModel:(UIViewController *)controller { ]; size_t len; char *m ...
- navigationcontroller和navigationbar和navigationitem之间的区别以及不用nib实现点击屏幕关闭虚拟键盘20130911
1.UIViewController UIView的关系. UIView是视图,UIViewController是视图控制器,两者之间是从属关系,当创建一个UIViewController的时候,一般 ...
- cf 1006E
#include <iostream> #include <cstdio> #include <cstring> #include <string> # ...
- cento命令之which、whereis、locate、find
[which] 查看可执行文件的位置 语法: [root@localhost ~]# which 可执行文件名称 例如: [root@localhost ~]# which passwd /usr/b ...
- SmartGit 30天评估期结束解决办法
smartgit 需要输入序列号解决办法: 1.找到路径: %APPDATA%\syntevo\SmartGit\<main-smartgit-version> 然后删除: setting ...
- vagrant中的precise64使用静态的能和host所在局域网的其他机器互相通信
vagrant中的precise64使用静态的能和host所在局域网的其他机器互相通信