实现思路:
①鼠标按下+鼠标移动 → 拖拽
②鼠标松开 → 无拖拽
③鼠标偏移 → 拖拽距离
用JavaScript事件方法表示就是:
① onmousedown + onmousemove → startDrag()
② onmouseup → stopDrag()
drag.js代码:
var params = {left: 0,
top: 0,
currentX: 0,
currentY: 0,
flag: false
}
//获取相关CSS属性
var getCss = function(o,key){
return o.currentStyle? o.currentStyle[key] : document.defaultView.getComputedStyle(o,false)[key]
}
//拖拽的实现
var startDrag = function(bar, target, callback){
if(getCss(target, "left") !== "auto"){
params.left = getCss(target, "left")
}
if(getCss(target, "top") !== "auto"){
params.top = getCss(target, "top")
}
//o是移动对象
bar.onmousedown = function(event){
params.flag = true
if(!event){
event = window.event
//防止IE文字选中
bar.onselectstart = function(){
return false
}
}
var e = event
params.currentX = e.clientX
params.currentY = e.clientY
}
document.onmouseup = function(){
params.flag = false
if(getCss(target, "left") !== "auto"){
params.left = getCss(target, "left")
}
if(getCss(target, "top") !== "auto"){
params.top = getCss(target, "top")
}
}
document.onmousemove = function(event){
var e = event ? event: window.event
if(params.flag){
var nowX = e.clientX, nowY = e.clientY
var disX = nowX - params.currentX, disY = nowY - params.currentY
target.style.left = parseInt(params.left) + disX + "px"
target.style.top = parseInt(params.top) + disY + "px"
}
if (typeof callback == "function") {
callback(parseInt(params.left) + disX, parseInt(params.top) + disY)
}
}
}
HTML/CSS
<style type="text/css">#box{position:absolute left:100px top:100px padding:5px background:#f0f3f9 font-size:12px -moz-box-shadow:2px 2px 4px #666666 -webkit-box-shadow:2px 2px 4px #666666}
#main{border:1px solid #a0b3d6 background:white}
#bar{line-height:24px background:#beceeb border-bottom:1px solid #a0b3d6 padding-left:5px cursor:move}
#content{width:420px height:250px padding:10px 5px}
</style>
<div id="box">
<div id="main">
<div id="bar">拖拽</div>
<div id="content">
内容……
</div>
</div>
</div>
JS部分
<script src="drag.js" type="text/javascript"></script><script type="text/javascript">
var oBox = document.getElementById("box")
var oBar = document.getElementById("bar")
startDrag(oBar, oBox)
</script>
<!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" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">
<title>Document</title>
</head>
<body>
<div id="box1" style = 'width:100pxheight: 100pxbackground:#cccposition:fixed'></div>
<!-- 注意 盒子一定要定位 position:fixed或者 position:absolute-->
</body>
<script type="text/javascript">
function getPos(e){//这是一个 获取鼠标位置的函数
var oEvent = e || event
return {x: oEvent.clientX + document.documentElement.scrollLeft || document.body.scrollLeft,
y: oEvent.clientY +document.documentElement.scrollTop || document.body.scrollTop}
}
document.getElementById('box1').onmousedown = function(e){ //你要拖动对象在mousedown的时候发生的事情
var oEvent = e || event
var pos = getPos(oEvent)
var _this = this
_this.style.cursor = 'move'//改变鼠标状态
_this.disY = pos.y - _this.offsetTop
_this.disX = pos.x - _this.offsetLeft
document.onmousemove =function(e){ //在鼠标按下的时候 并且 移动的时候 发生的事情
var oEvent = e || event
var dpos = getPos(oEvent)
var t = dpos.y-_this.disY //移动的时候获取的 移动 top值
var l = dpos.x-_this.disX //移动的时候获取的 移动 left
_this.style.top=t + "px" //这两条给盒子赋值
_this.style.left=l + "px"
}
document.onmouseup = function(){//鼠标弹起的时候做的事情 一些释放 操作
_this.style.cursor = 'pointer'
this.onmousemove = null
this.onmouseup = null
try{
_this.releaseCapture()}catch(e){}
}
try{
_this.setCapture()}catch(b){} //这里是为了 让盒子拖动的时候不要复制页面里面的其他内容
return false
}
</script>
</html>