老师请解答
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 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" /> <meta http-equiv= "X-UA-Compatible" content= "IE=edge" /> <meta name= "viewport" content= "width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .box { width: 200px; height: 200px; background-color: red; } </style> </head> <body style= "height: 2000px" > <div class= "box" id= "box" ></div> <script> const drag = EL => { const startPoint = {}; // 每次拖拽最开始的触摸点 const movePoint = {}; // 拖拽过程中移动的点 const curPos = { // 被拖拽元素的当前位置 x: 0, y: 0, }; EL.addEventListener( "pointerdown" , startHandler, false ); EL.addEventListener( "pointermove" , moveHandler, false ); EL.addEventListener( "pointerup" , endHandler, false ); EL.addEventListener( "pointercancel" , endHandler, false ); function startHandler(e) { e.preventDefault(); startPoint.x = e.pageX; startPoint.y = e.pageY; } function moveHandler(e) { movePoint.x = curPos.x + e.pageX - startPoint.x; movePoint.y = curPos.y + e.pageY - startPoint.y; EL.style.transform = `translate3d(${movePoint.x}px, ${movePoint.y}px, 0)`; } function endHandler() { curPos.x = movePoint.x; curPos.y = movePoint.y; } }; drag(document.getElementById( "box" )); </script> </body> </html> |
11
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧