Javascript 简单实现鼠标拖动DIV
http://zhangbo-peipei-163-com.iteye.com/blog/1740078
比较精简的Javascript拖动效果函数代码
http://www.jb51.net/article/10578.htm
<html>
<head><title>拖动效果函数演示 by Longbill.cn</title>
<style>
body
{
font-size:12px;
color:#333333;
border : 0px solid blue;
}
div
{
position : absolute;
background-color : #c3d9ff;
margin : 0px;
padding : 5px;
border : 0px;
width : 100px;
height:100px;
}
</style>
</head>
<body>
<script>
function drag(o,s)
{
if (typeof o == "string") o = document.getElementById(o);
o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
o.orig_index = o.style.zIndex; o.onmousedown = function(a)
{
this.style.cursor = "move";
this.style.zIndex = 10000;
var d=document;
if(!a)a=window.event;
var x = a.clientX+d.body.scrollLeft-o.offsetLeft;
var y = a.clientY+d.body.scrollTop-o.offsetTop;
//author: www.longbill.cn
d.ondragstart = "return false;"
d.onselectstart = "return false;"
d.onselect = "document.selection.empty();" if(o.setCapture)
o.setCapture();
else if(window.captureEvents)
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP); d.onmousemove = function(a)
{
if(!a)a=window.event;
o.style.left = a.clientX+document.body.scrollLeft-x;
o.style.top = a.clientY+document.body.scrollTop-y;
o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
} d.onmouseup = function()
{
if(o.releaseCapture)
o.releaseCapture();
else if(window.captureEvents)
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
d.onmousemove = null;
d.onmouseup = null;
d.ondragstart = null;
d.onselectstart = null;
d.onselect = null;
o.style.cursor = "normal";
o.style.zIndex = o.orig_index;
}
} if (s)
{
var orig_scroll = window.onscroll?window.onscroll:function (){};
window.onscroll = function ()
{
orig_scroll();
o.style.left = o.orig_x + document.body.scrollLeft;
o.style.top = o.orig_y + document.body.scrollTop;
}
}
}
</script> <div id="div1" style="left:10px;top:10px;">div1:我可以被拖动</div>
<div id="div2" style="left:120px;top:10px;background-color : #f3d9ff">div2:来拖我呀</div>
<div id="div3" style="left:230px;top:10px;background-color : #c3ffff">div3:我随便你拖</div>
<div id="div4" style="left:10px;top:120px;background-color : #c3d944">div4:我可以随窗口滑动,把我拖到最下面,然后滚动网页看看</div>
<div id="div5" style="left:120px;top:120px;background-color : #f3d944">作者: Longbill
<a href=http://www.longbill.cn target=_blank>www.longbill.cn</a>
</div>
<div id="div6" style="left:230px;top:120px;background-color : #e3f944;width:200px;">参数说明: drag(obj [,scroll]); obj:对象的id或对象本身; scroll(可选):对象是否随窗口拖动而滑动,默认为否 鼠标右键查看源代码
</div> <script>
drag("div1");
drag("div2");
drag("div3");
drag("div4",1);
drag("div5",1);
drag("div6",1); </script> </body>
avascript 简单实现鼠标拖动DIV
博客分类:
要想实现鼠标拖动效果,免不了要计算元素在浏览器中的坐标,那首先来学习一下各种坐标。
参考:
Javascript获取页面的各种坐标汇总
实现拖动:
1.定义需要的变量
- var bool=false,
- pageX=0,
- pageY=0,
- //需要拖动的目标DIV
- element = $("#tb_window"),
- eWidth = element.width(),
- eHeight = element.height(),
- //在该DIV的范围内拖动
- pElement = $("#flashFrame"),
- pWidth = pElement.width(),
- pHeight = pElement.height();
2.鼠标mousedown事件计算鼠标焦点x相对目标DIV的坐标
- element.mousedown(function(event)
- {
- needMove=true;
- var position = element.position();
- pageX = event.pageX-position.left; //鼠标和DIV的相对坐标
- pageY = event.pageY-position.top;
- element.css('cursor','move');
- });
3.鼠标mouseup事件将变量needMove赋值false,表示不需要移动DIV
- element.mouseup(function(event)
- {
- needMove=false;
- });
4.鼠标的mousemove事件开始移动目标DIV
- element.mousemove(function(event)
- {
- if(!needMove) {return;}
- //鼠标在页面的坐标 - 鼠标和DIV的相对坐标 = DIV在页面的坐标
- var ePageX = event.pageX;
- var ePageY = event.pageY;
- var x = ePageX-pageX;
- var y = ePageY-pageY;
- if (ePageX <= eWidth/ 2 || ePageX >= pWidth - eWidth / 2)
- {
- return;
- }
- if (ePageY< eHeight / 2 || ePageY >= pHeight - eHeight / 2)
- {
- return;
- }
- element.css("left", x);
- element.css("top", y);
- });
5.总结,以上代码只是简单实现鼠标拖动DIV的思路(当然功能也是可以的),具体的性能,兼容性和代码扩展性都有待商榷。比如:还可以增加参数控制DIV在指定的大小范围内拖动等。
Javascript 简单实现鼠标拖动DIV的更多相关文章
- JavaScript鼠标拖动div且可调整div大小
http://www.softwhy.com/article-5502-1.html <!DOCTYPE html> <html> <head> <meta ...
- 鼠标拖动div,div跟随鼠标移动效果
<div id="boxDiv" style='width:20px;height:20px;position:absolute;background:red;'> ...
- 鼠标拖动DOM
自己收藏,使用angualrjs的directive些的鼠标拖动DOM.... <!DOCTYPE html> <html lang="en"> <h ...
- 简单的鼠标可拖动div 兼容IE/FF
来源:http://www.cnblogs.com/imwtr/p/4355416.html 作者: 主要思路: 一个div,注册监听onmousedown事件,然后处理获取的对象及其相关值(对象高度 ...
- 鼠标拖动改变DIV等网页元素的大小的最佳实践
1.初次实现 1.1 html代码 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" la ...
- javascript中区分鼠标单击和拖动事件
在javascript中,一般的DOM元素如div,都有onmousedown.onmousemove.onmouseup这3个鼠标事件. <div id="div1" on ...
- jQuery实现鼠标拖动改变Div高度
最近项目中需要在DashBoard页面做一个事件通知栏,该通知栏固定位于页面底部,鼠标拖动该DIV实现自动改变高度扩展内容显示区域. 以下是一个设计原型,基于jQuery实现,只实现了拖动效果,没有做 ...
- as3用鼠标拖动图形拼图——灰常简单的教程
话说这种效果在课件里面经常用到,鼠标拖动事件,一个运用很频繁的事件,一起学习学习吧 首先SWF贡献给大家看看效果 感觉咋样,原理其实还蛮简单的,做做试试吧 下面来看看源码吧 package { imp ...
- javascript拖动div
div拖动代码,在用此代码之前,你可能需要将你需要拖动的元素style设置position: absolute; #textareaSavaDiv{ position: absolute; right ...
随机推荐
- 《C#多线程编程实战》2.8 Barrier
不得不说,C#的同步线程的机制是真的多. 各式各样.几乎各种场景下都有可以使用的同步机制. 今天说的,就是比较有意思了. 等待的机制很简单,单纯的等待. 使用的方法我就等. 等待的东西或者内容则是你自 ...
- Web渗透测试(xss漏洞)
Xss介绍—— XSS (cross-site script) 跨站脚本自1996年诞生以来,一直被OWASP(open web application security project) 评为十大安 ...
- Navicat 远程连接 MySQL
Navicat 远程连接 MySQL 相信大家都有在远程服务器上进行开发吧,其中 MySQL 的使用率应该也会挺高,如果使用 Navicat 等可视化工具来操作远程数据库不失为一种很好的选择,避免了在 ...
- 【spring】InitializingBean接口
apollo 源码中有这么一个类 public class ReleaseMessageScanner implements InitializingBean @Override public voi ...
- 5.EM
- 002 android studio 常用设置
1.改变字体 file--->setting --->font--->size 2.更改最小安卓版本 在project目录下,app下的build.gradle中修改 注意:buil ...
- 一种简单快速的模板解析方法,活用with javascript版
//一种简单快速的模板解析方法,活用with var parseTpl = function( str, data ) { var tmpl = 'var __p=[];' + 'with(obj|| ...
- mongodb的初步使用
一.mongodb简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产 ...
- nodejs的一些学习
要使用npm的时候,其实是可以直接下载node.js的.参考文档http://www.runoob.com/nodejs/nodejs-npm.html 安装成功之后.判断是否安装成功.是不能直接用n ...
- linux 第八章 高级键盘
1.clear:清屏 2.history:显示历史命令列表 3.Ctrl+A:移动光标到行首 4.Ctrl+E:移动光标到行尾 5.Ctrl+F:光标向前移动一个字符 6.Ctrl+B:光标向h后移动 ...