文字与图片被拖曳

<html>
<head>
<title>文字与图片被拖曳</title>
<style type="text/css">
*{padding:0;margin:0;}
.tips{position:absolute;background:#eee;}
</style>
</head>
<body>
<div class="tips" id="tips1" onmouseover="dragF.drag('tips1');">
<img src="/images/logo.gif">拖动图片</div>
<div class="tips" id="tips2" onmouseover="dragF.drag('tips2');"><a href="http://www.codefans.net" target="_blank">链接</a><br /> 拖动链接也可以
</div>
</body>
<script type="text/javascript">
var $id=function(id){return document.getElementById(id);}
var dragF={
locked:false,
lastObj:undefined,
drag:function(obj){
$id(obj).onmousedown=function(e){
var e = e ? e : window.event;
if(!window.event) {e.preventDefault();}/* 阻止标注浏览器下拖动a,img的默认事件 */
dragF.locked=true;
  $id(obj).style.position="absolute";
$id(obj).style.zIndex="100";
if (dragF.lastObj&&dragF.lastObj!=$id(obj)) {/* 多元素拖动时候需要恢复上一次元素的状态 */
dragF.lastObj.style.zIndex = "1";
}
dragF.lastObj=$id(obj);
var tempX=$id(obj).offsetLeft;
var tempY=$id(obj).offsetTop;
dragF.x=e.clientX;
dragF.y=e.clientY;
document.onmousemove=function(e){
var e = e ? e : window.event;
if(dragF.locked==false) return false;
$id(obj).style.left=tempX+e.clientX-dragF.x+"px";
$id(obj).style.top=tempY+e.clientY-dragF.y+"px";
if(window.event) {e.returnValue=false;}/* 阻止ie下a,img的默认事件 */
}
document.onmouseup=function(){
dragF.locked=false;
}
}
}
}
</script>
</html>

你可能感兴趣的:(浏览器,IE)