通过控制div的left和top,来让随意拖拽和拉伸,听上去是不是很好玩呢,接下来就跟随我的脚步来了解一下吧,web前端技术
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Author" content="FengYu"> <title>爱前端博客</title> <style> *{margin:0;padding:0;font-family: Microsoft YaHei,serif;} li{list-style: none;} #box{ position: absolute; top:100px; left:100px; width:100px; height: 100px; background: pink; } #sbox{ position: absolute; bottom:0; right:0; width:5px; height: 5px; background: red; } </style> <!--<script src="https://git.oschina.net/cx-fy/personal/raw/master/conmenu.js"></script>--> </head> <body> <div id="box"> <div id="sbox"></div> </div> <script> box.onmousedown=function (e) { e=e||window.event; var xDown = e.clientX,//获取鼠标的初始坐标 yDown = e.clientY,//获取鼠标的初始坐标 leftDown = this.offsetLeft, topDown = this.offsetTop; box.style.cursor = "pointer" document.onmousemove=function (e) { e = e||window.event; var xMove = e.clientX, yMove = e.clientY; box.style.left = leftDown + xMove - xDown + "px"; box.style.top =topDown + yMove - yDown + "px"; } }; document.onmouseup=function () { box.style.cursor = "default"; document.body.style.cursor = "default"; this.onmousemove=null; } sbox.onmousedown=function (e) { e = e||window.event; e.cancelBubble=true; var xDown = e.clientX, yDown = e.clientY, boxW = box.clientWidth, boxH = box.clientHeight; document.body.style.cursor = "se-resize"; document.onmousemove=function (e) { e = e||window.event; var xMove = e.clientX, yMove = e.clientY, x_ = xMove - xDown, //x变化量 y_ = yMove - yDown, // y变化量 width = Math.max(10,x_+boxW), height = Math.max(10,y_+boxH); box.style.width = width + "px"; box.style.height = height + "px"; } } </script> </body> </html> |
7