下面我将为你详细讲解JS的touch事件实际引用的攻略。
下面我将为你详细讲解JS的touch事件实际引用的攻略。
一、什么是Touch事件?
Touch事件是一种移动端特有的事件,它包括了以下几个事件:
- touchstart: 手指触摸屏幕时触发的事件
- touchmove: 手指在屏幕上滑动时触发的事件
- touchend: 手指从屏幕上离开时触发的事件
- touchcancel: 触摸被意外取消时触发的事件,如页面被弹框打断
二、Touch事件的实际引用
Touch事件的实际引用非常广泛,下面给出两个示例说明:
1. 点击事件
采用Touch事件来处理点击事件可以避免设备的300毫秒点击延迟,提高用户的体验。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Touch事件示例 - 点击事件</title>
<style>
#box {
width: 200px;
height: 200px;
background-color: gray;
text-align: center;
line-height: 200px;
font-size: 20px;
color: white;
}
</style>
</head>
<body>
<div id="box">点击我</div>
<script>
var box = document.getElementById('box');
var isMoving = false;
box.addEventListener('touchstart', function(e) {
isMoving = false;
});
box.addEventListener('touchmove', function(e) {
isMoving = true;
});
box.addEventListener('touchend', function(e) {
if (!isMoving) {
alert('点击了盒子!');
}
});
</script>
</body>
</html>
2. 触摸滑动事件
利用Touch事件可以轻松实现触摸滑动事件,通过计算触摸事件的偏移量来实现页面的平滑移动。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Touch事件示例 - 触摸滑动事件</title>
<style>
#box {
width: 100%;
height: 300px;
overflow-x: hidden;
white-space: nowrap;
font-size: 0;
}
#box > img {
width: 100%;
display: inline-block;
}
</style>
</head>
<body>
<div id="box">
<img src="https://picsum.photos/id/100/800/300" alt="">
<img src="https://picsum.photos/id/200/800/300" alt="">
<img src="https://picsum.photos/id/300/800/300" alt="">
</div>
<script>
var box = document.getElementById('box');
var start = 0;
var end = 0;
box.addEventListener('touchstart', function(e) {
start = e.touches[0].pageX;
});
box.addEventListener('touchmove', function(e) {
end = e.touches[0].pageX;
var offset = end - start;
box.style.transform = 'translateX(' + offset + 'px)';
});
box.addEventListener('touchend', function(e) {
start = 0;
end = 0;
box.style.transform = 'translateX(0)';
});
</script>
</body>
</html>
以上就是Touch事件实际引用的攻略,希望对您有所帮助!
沃梦达教程
本文标题为:js的touch事件的实际引用


猜你喜欢
- 为JS扩展Array.prototype.indexOf引发的问题探讨及解决 2023-12-23
- javascript – 如何从HTML表格中将数据插入mysql数据库 2023-10-26
- http://www.sky.franken.de/doxy/explorer/structIShellBrowserImpl.html 2023-10-25
- 手机安装GreasyFork油猴js脚本的教程 2023-08-11
- 使用HTML5中postMessage知识点解决Ajax中POST跨域问题 2022-10-17
- ajax上传多图到php服务器的方法 2023-02-15
- 深入探讨CSS中字体元素 2022-10-16
- javascript – 我希望在命令行上获得我的linux设备的准确纬度经度.就像HTML5中的Geolocation一样.我没有访问浏览器 2023-10-25
- JavaScript中windows.open()、windows.close()方法详解 2023-11-30
- 关于Ajax技术中servlet末尾的输出流 2023-01-21