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简单的实现鼠标拖动DIV的效果。没有什么技术含量,全当笔记。
要想实现鼠标拖动效果,免不了要计算元素在浏览器中的坐标,那首先来学习一下各种坐标。
参考:
Javascript获取页面的各种坐标汇总
实现拖动:
1.定义需要的变量
Javascript代码
- 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的坐标
Javascript代码
- 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
Javascript代码
- element.mouseup(function(event)
- {
- needMove=false;
- });
4.鼠标的mousemove事件开始移动目标DIV
Javascript代码
- 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在指定的大小范围内拖动等。
时间: 2024-10-06 17:14:24