This article describes the method of JavaScript to implement text and image drag and drop effects. Share it for your reference. The specific implementation method is as follows:
Copy the code as follows: <html>
<head>
<title>JavaScript realizes the drag and drop effect of text and pictures</title>
<style type="text/css">
*{padding:0;margin:0;}
.tips{position:absolute;background:#eee;}
</style>
</head>
<body>
<div id="tips1" onmouseover="dragF.drag('tips1');">
<img src="/images/skinslogo.gif"><br>Image can be dragged</div>
<div id="tips2" onmouseover="dragF.drag('tips2');"><a href="//www.VeVB.COM" target="_blank">Wulin.com</a><br />Drag links are also OK
</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();}/* Prevent the default event of a,img in the annotation <a href='/site/js-5791-1.html' target='_blank'><u>Drag a,img under the browser</u></a>*/
dragF.locked=true;
$id(obj).style.position="absolute";
$id(obj).style.zIndex="100";
if (dragF.lastObj&&dragF.lastObj!=$id(obj)) {/* Multi-element drag requires restoration of the last element state*/
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;}/* Block the default event of a,img under ie*/
}
document.onmouseup=function(){
dragF.locked=false;
}
}
}
}
</script>
</html>
I hope this article will be helpful to everyone's JavaScript programming.