js实现 鼠标拖动背景图片
<html>
<head>
<style type="text/Css">
.dragme{position:relative;}
</style>
<script type="text/javascript">
//alert(document.getElementById("moveid").style.left);
var ie=document.all;
var nn6=document.getElementById&&!document.all;
var isdrag=false;
var x,y;
var dobj;
var getX;
var getY;
function movemouse(e){
if (isdrag){
dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
dobj.style.top = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
//alert(getX);
// alert(getY);
return false;
}
}
function selectmouse(e){
/*
var fobj = nn6 ? e.target : event.srcElement;
var topelement = nn6 ? "Html" : "BODY";
while (fobj.tagName != topelement && fobj.className != "dragme") {
fobj = nn6 ? fobj.parentNode : fobj.parentElement;
}
*/
fobj = document.getElementById("moveid")
if (fobj.className=="dragme") {
isdrag = true;
dobj = fobj;
tx = parseInt(dobj.style.left+0);
ty = parseInt(dobj.style.top+0);
x = nn6 ? e.clientX : event.clientX;
y = nn6 ? e.clientY : event.clientY;
document.onmousemove=movemouse;
return false;
}
}
document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");
//得到元素的坐标位置
function findPosition(id)
{
oElement = document.getElementById(id)
var x2 = 0;
var y2 = 0;
var width = oElement.offsetWidth; //得到元素的宽度
var height = oElement.offsetHeight;
//alert(width + "=" + height);
if( typeof( oElement.offsetParent ) != 'undefined' )
{
for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent )
{
posX += oElement.offsetLeft;
posY += oElement.offsetTop;
}
x2 = posX + width;
y2 = posY + height;
//alert("sun:"+ posX);
//alert(posY);
// return [ posX, posY ,x2, y2];
} else{
x2 = oElement.x + width;
y2 = oElement.y + height;
alert(oElement.x);
alert(oElement.y);
//return [ oElement.x, oElement.y, x2, y2];
}
getX = posX - 200;
getY = posY - 10;
alert(getX);
alert(getY);
}
</script>
</head>
<body>
<div align="left" style="position:absolute;z-index:1;left: 200px;top: 10px;"><img src="upload/vista.jpg" id="moveid" class="dragme" ></div>
<div align="center" id="nav" style="position:absolute;z-index:999;width:800px;height:600px;overflow:hidden;background:url(template/1.gif) no-repeat;left: 200px;top: 10px;">
<input name="" type="button" value="提交" onClick= "findPosition('moveid')">
</div>
<script language="javascript">
//var obj = document.getElementById("moveid");
//findPosition(obj);
//obj = document.getElementById("nav")