触屏事件
触屏事件概述:
移动端浏览器兼容性较好,我们不需要考虑以前JS的兼容性问题,可以放心的使用原生JS书写效果,但是移动端也有自己独特的地方。比如触屏事件touch (也称触摸事件),Android和IOS都有心
代码示例:
<!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> div { width: 100px; height: 100px; background-color: pink; } </style> </head> <body> <div></div> </body> <script> // 获取元素 var div = document.querySelector('div') div.addEventListener('touchstart', function () { console.log('被触摸'); }) div.addEventListener('touchmove', function () { console.log('被移动'); }) div.addEventListener('touchend', function () { console.log('被抛弃'); }) </script> </html>