<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="description"
content="Click here for a script that helps enable drag and drop on any image in the page!" />
<meta name="keywords" content="DHTML, drag and drop script, JavaScript, free" />
<title>Dynamic Drive DHTML Script- Dragable elements Script</title>
<link rel="stylesheet" type="text/css" href="../ddincludes/mainstyle.css" />
<style type="text/css">
.drag{
position:relative;
cursor:pointer;
z-index: 100;
}
</style>
<script type="text/javascript">
var dragobject={
z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null, dragapproved : 0,
initialize:function(){
document.οnmοusedοwn=this.drag
document.οnmοuseup=function(){this.dragapproved=0}
},
drag:function(e){
var evtobj=window.event? window.event : e
this.targetobj=window.event? event.srcElement : e.target
if (this.targetobj.className=="drag"){
this.dragapproved=1
if (isNaN(parseInt(this.targetobj.style.left))){this.targetobj.style.left=0}
if (isNaN(parseInt(this.targetobj.style.top))){this.targetobj.style.top=0}
this.offsetx=parseInt(this.targetobj.style.left)
this.offsety=parseInt(this.targetobj.style.top)
this.x=evtobj.clientX
this.y=evtobj.clientY
if (evtobj.preventDefault)
evtobj.preventDefault()
document.οnmοusemοve=dragobject.moveit
}
},
moveit:function(e){
var evtobj=window.event? window.event : e
if (this.dragapproved==1){
this.targetobj.style.left=this.offsetx+evtobj.clientX-this.x+"px"
this.targetobj.style.top=this.offsety+evtobj.clientY-this.y+"px"
return false
}
}
}
dragobject.initialize()
</script>
</head>
<body>
<h1><b class="drag">用鼠标拖动DIV或图象(image)的JS源码</b></h1>
<img src="9.jpg" class="drag"><br>
</body>
</html>
鼠标拖动图片【兼容各个浏览器】
最新推荐文章于 2020-05-24 18:55:43 发布