This article summarizes some of the effects extended by dragging for your reference. The specific content is as follows
1. Realize the framed effect when dragging pictures. That is, when the mouse drags a picture or object, its original position retains its shape.
This effect is actually very simple. It mainly involves creating another object with the same width and height as the dragged object, and then changing it into the dragged object.
Go directly to the code:
<html <head> <style> #div1 {width:100px; height:100px; background:yellow; position:absolute;} .box{border: 1px solid black;position: absolute;} </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script> window.onload=function ()//凡是被拖拽的物体,其必须定位为absolute { var oDiv=document.getElementById('div1'); oDiv.onmousedown=function (ev) { var oEvent=ev||event; var disX=oEvent.clientX-oDiv.offsetLeft; var disY=oEvent.clientY-oDiv.offsetTop; var oNewDiv=document.createElement('div'); oNewDiv.className='box'; oNewDiv.style.width=oDiv.offsetWidth-2+'px';//将2px的边框去掉 oNewDiv.style.height=oDiv.offsetHeight-2+'px'; oNewDiv.style.left=oDiv.offsetLeft+'px'; oNewDiv.style.top=oDiv.offsetTop+'px'; document.body.appendChild(oNewDiv); document.onmousemove=function (ev) { var oEvent=ev||event; oNewDiv.style.left=oEvent.clientX-disX+'px'; oNewDiv.style.top=oEvent.clientY-disY+'px'; }; document.onmouseup=function () { document.onmousemove=null; document.onmouseup=null; document.body.removeChild(oNewDiv); oDiv.style.left=oNewDiv.style.left; oDiv.style.top=oNewDiv.style.top; }; }; }; </script> </head> <body> <div id="div1"> </div> </body> </html>
2. Regarding the effect of dragging the window to zoom in and out, just wrap another div in the above div.
<html <head> <style> #div1 {width:10px; height:10px; background:url(images/1.gif); position:absolute;bottom: 0;right: 0}//拖拉的物体,改为图片 #div2{width: 200px;height: 200px;position: relative;background: #ccc;} .box{border: 1px solid black;position: absolute;} </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script> window.onload=function ()//凡是被拖拽的物体,其必须定位为absolute { var oDiv=document.getElementById('div1'); var oDiv2=document.getElementById('div2'); oDiv.onmousedown=function (ev) { var oEvent=ev||event; var disX=oEvent.clientX-oDiv.offsetLeft; var disY=oEvent.clientY-oDiv.offsetTop; var oNewDiv=document.createElement('div'); //oNewDiv.className='box'; oNewDiv.style.width=oDiv.offsetWidth-2+'px'; oNewDiv.style.height=oDiv.offsetHeight-2+'px'; oNewDiv.style.left=oDiv.offsetLeft+'px'; oNewDiv.style.top=oDiv.offsetTop+'px'; document.body.appendChild(oNewDiv); document.onmousemove=function (ev) { var oEvent=ev||event; oDiv2.style.width=oEvent.clientX-disX+'px';//这里是它的父级 oDiv2.style.height=oEvent.clientY-disY+'px'; }; document.onmouseup=function () { document.onmousemove=null; document.onmouseup=null; document.body.removeChild(oNewDiv); oDiv.style.left=oDiv2.style.left; oDiv.style.top=oDiv2.style.top; }; }; }; </script> </head> <body> <div id='div2'> <div id="div1"> </div> </div> </body> </html>
The above is the entire content of this article, I hope it will be helpful to everyone’s study.