移动端事件

移动端事件有两类:

触屏(常用)

指针(了解,兼容性问题很大)

//触屏  指针
		/*
			touchstart(手指按下)	类似于pc端的mousedown
			touchmove(手指移动)		类似于pc端的mousemove
				不可能单独触发	可以单独触发
			touchend(手指抬起)		类似于pc端的mouseup
		*/

触屏事件不要使用dom0绑定(低版本的没效果)

var item = document.querySelector(".item");
			item.addEventListener("touchstart",function(){
				console.log("touchstart")
			})
			item.addEventListener("touchmove",function(){
				console.log("touchmove")
			})
			item.addEventListener("touchend",function(){
				console.log("touchend")
			})

触屏事件的默认行为:

​ 1.手指长按可以复制

​ 2.下滑有橡皮筋效果

但是,为了触屏事件干净需要禁止事件的默认行为

document.addEventListener("touchstart",function(ev){
				ev=ev||event;
				ev.preventDefault();//阻止系统触屏事件的默认行为
			})
			
			这样可以使某个元素具有默认行为
			/*var item = document.querySelector(".item");
			item.addEventListener("touchstart",function(ev){
				ev=ev||event;
				ev.stopPropagation();
			})*/
			
			/*var item = document.querySelector(".item");
			item.addEventListener("touchstart",function(ev){
				ev=ev||event;
				console.log(ev.cancelable);
				ev.preventDefault();
			})*/

隐患: 全面禁止了触屏事件的默认行为就不会有系统默认滚动条,所以需要自己手写滚动条

事件点透

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no" />
		<title></title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			.item{
				position: absolute;
				left: 0;
				top: 0;
				width: 200px;
				height:200px;
				background: pink;
				opacity: .5;
			}
			
		</style>
	</head>
	<body>
		<div class="item"></div>
		<a href="https://www.baidu.com/">去百度</a>
	</body>
	<script type="text/javascript">
		window.onload=function(){
			/*
				1.pc端的事件可以在移动端触发
				2.PC端事件有300毫秒延迟
				3.移动端事件不会有延迟
				
				
				如果要达到这种目的就使用pc端的click事件
			*/
			var item = document.querySelector(".item");		
			var a = document.querySelector("a");
            
			item.addEventListener("click",function(){
				console.log("click");
			})
            
			//touchstart事件立马执行, 等300ms看是否是双击,如果是单击的就会触发下面的a标签跳转
			item.addEventListener("touchstart",function(){
				this.style.display="none";
			})
            
			a.addEventListener("click",function(){
				console.log("click");
			})
			
		}
		
		
	</script>
</html>

移动端滑动防误触方案

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no" />
		<title></title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			
		</style>
	</head>
	<body>
		<a href="https://www.baidu.com/">去百度</a>
		<a href="https://www.baidu.com/">去百度</a>
		<a href="https://www.baidu.com/">去百度</a>
		<a href="https://www.baidu.com/">去百度</a>
		<a href="https://www.baidu.com/">去百度</a>
		<a href="https://www.baidu.com/">去百度</a>
	</body>
	<script type="text/javascript">
		window.onload=function(){
			document.addEventListener("touchstart",function(ev){
				ev=ev||event;
				ev.preventDefault();
			})
			
			
			//移动端a标签的跳转方案  解决误触
			var aNodes = document.querySelectorAll("a");
			for(var i=0;i<aNodes.length;i++){
				aNodes[i].addEventListener("touchstart",function(){
					this.isMoved=false;
				})
				
				
				aNodes[i].addEventListener("touchmove",function(){
					this.isMoved=true;
				})
				
				
				aNodes[i].addEventListener("touchend",function(){
					if(!this.isMoved){
						location.href=this.href;
					}
				})
			}
		}
		
		
	</script>
</html>

触屏事件对象的3个重要属性

changedTouches:触发当前事件的手指列表
targetTouches:触发当前事件时元素上剩余的手指列表
touches:触发当前事件时屏幕上剩余的手指列表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
移动端双指事件通常是用于实现缩放或者旋转操作的,常见的事件有touchstart、touchmove、touchend等。以下是一些常用的双指事件: 1. touchstart事件 当有两个手指同时放在屏幕上时,会触发touchstart事件。此时可以通过e.touches来获取所有手指的信息,如下所示: ```javascript document.addEventListener('touchstart', function(e) { if (e.touches.length === 2) { // 获取第一个手指的坐标 var x1 = e.touches[0].clientX; var y1 = e.touches[0].clientY; // 获取第二个手指的坐标 var x2 = e.touches[1].clientX; var y2 = e.touches[1].clientY; // 其他操作 } }, false); ``` 2. touchmove事件 当两个手指在屏幕上移动时,会触发touchmove事件。此时可以通过e.touches来获取所有手指的信息,如下所示: ```javascript document.addEventListener('touchmove', function(e) { if (e.touches.length === 2) { // 获取第一个手指的坐标 var x1 = e.touches[0].clientX; var y1 = e.touches[0].clientY; // 获取第二个手指的坐标 var x2 = e.touches[1].clientX; var y2 = e.touches[1].clientY; // 其他操作 } }, false); ``` 3. touchend事件 当两个手指中的任意一个离开屏幕时,会触发touchend事件。此时可以通过e.touches来获取所有手指的信息,如下所示: ```javascript document.addEventListener('touchend', function(e) { if (e.touches.length === 1) { // 只有一个手指,可以进行单指操作 } }, false); ``` 通过以上三个事件,我们可以实现双指操作的各种需求,比如缩放、旋转等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值