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 ...
随机推荐
- LeetCode(129) Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- linux学习-CentOS 7 环境下大量建置账号的方法
一些账号相关的检查工具 pwck pwck 这个指令在检查 /etc/passwd 这个账号配置文件内的信息,与实际的家目录是否存在等信息, 还可以比对 /etc/passwd /etc/shadow ...
- 使用html+javascriptt实现的简易四则运算(初学JavaScript笔记)
今天第一天学javascript,做了个简易的四则运算,提供参考,效果图: html代码: <!DOCTYPE html> <html > <head > < ...
- flask-用户资料
首先创建User模型 class User(UserMixin,db.Model): __tablename__ = 'users' #.. name = db.Column(db.String(64 ...
- [python学习篇] [os模块] [2]删除文件夹
def deleteDirectory(self,current_path): if not os.path.exists(current_path): self.logger.info(curren ...
- PHP杂技(一)
逻辑运算符 &&和& ||和|的部分区别 返回结果类型不同, A||B 如果A为真那么B不会做判断,而A|B前后都做判断 switch判断中并不是===,更像是==,例如(1) ...
- 【bzoj3676】[Apio2014]回文串 回文自动机
题目描述 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最大出现值. 输入 输入只有一行,为一个只包含小写字母( ...
- [OJ#63]树句节够提
[OJ#63]树句节够提 试题描述 给定一棵节点数为 N 的有根树,其中 1 号点是根节点,除此之外第 i 个节点的父亲为 fi.每个节点有一个权值 Ai,所有边权均为 1. 给定 Q 个询问,每个询 ...
- windows命令总结
工作中还是经常使用windows系统,将windows中常用的命令进行总结. 1. 从命令行打开资源管理器,即文件夹 start . 2. 查看端口占用情况 比如查看3000端口的占用情况 netst ...
- 刷题总结——String painter(hdu2476)
题目: Problem Description There are two strings A and B with equal length. Both strings are made up of ...