老师请检查
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 | <!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> <div class= "box" id= "box" ></div> <p id= "start" ></p> <p id= "mobile" ></p> <p id= "end" ></p> <script> // 获取DOM元素 const boxEL = document.getElementById( "box" ); const startEL = document.getElementById( "start" ); const mobileEL = document.getElementById( "mobile" ); const endEL = document.getElementById( "end" ); //定义函数startHandler 直接在事件处理函数中调用即可 const startHandler = e => { // 获取event对象的属性changedTouches保存到touch const touch = e.changedTouches[0]; // 往p标签里面填充HTML元素,获取按下时 相对于HTML文档的X、Y坐标并取整 startEL.innerHTML = `touch开始的位置: ${parseInt( touch.pageX )}.${parseInt(touch.pageY)}`; }; // 往p标签里面填充HTML元素,获取移动时 相对于HTML文档的X、Y坐标并取整 const moveHandler = e => { const touch = e.changedTouches[0]; mobileEL.innerHTML = `touch移动的位置: ${parseInt( touch.pageX )}.${parseInt(touch.pageY)}`; }; // 往p标签里面填充HTML元素,获取松开时 相对于HTML文档的X、Y坐标并取整 const endHandler = e => { const touch = e.changedTouches[0]; endEL.innerHTML = `touch结束的位置: ${parseInt(touch.pageX)}.${parseInt( touch.pageY )}`; }; // 绑定事件处理函数 boxEL.addEventListener( "touchstart" , startHandler, false ); boxEL.addEventListener( "touchmove" , moveHandler, false ); boxEL.addEventListener( "touchend" , endHandler, false ); </script> </body> </html> |
7
收起
正在回答
2回答
同学你好,代码效果正确,很棒,祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧