谷粒音乐day02_事件基础_(一个隐患_事件点透_移动端跳转方案_event)

一个隐患

在这里插入图片描述

<!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{
				width: 200px;
				height: 200px;
				background: pink;
				font-size: 30px;
				text-align: center;
				line-height: 200px;
				overflow: auto;
			}
		</style>
	</head>
	<body>
		<div id="item">
			qqqqqqqqqqqqqqqqqq
		</div>
	</body>
	<script type="text/javascript">
		window.onload=function(){
			/*
				1.meta 标签
				2.全面阻止事件的默认行为
					隐患:页面上所有的滚动条失效
					-->自定义更牛逼的滚动条
				3.一个适配方案
			*/
			document.addEventListener("touchstart",function(ev){
				ev=ev||event;
				ev.preventDefault();
			})
			
			;(function(){
				var styleNode = document.createElement("style");
				var w = document.documentElement.clientWidth/16;
				styleNode.innerHTML="html{font-size:"+w+"px!important}"
				document.head.appendChild(styleNode);
			})()
		}
		
	</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;
			}
			.item{
				position: absolute;
				left: 0;
				top: 0;
				width: 200px;
				height: 200px;
				background: pink;
				opacity: .5;
			}
		</style>
	</head>
	<body>
		<div class="item"></div>
		<a href="http://www.atguigu.com">回娘家</a>
	</body>
	<script type="text/javascript">
		window.onload=function(){
			/*
			1.pc端事件可以在移动端触发
			2.pc端事件有300毫秒延迟
			3.移动端事件不会有延迟
			*/
			var item = document.querySelector(".item");
			item.addEventListener("touchstart",function(){
				this.style.display="none";
			})
		}
		
	</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="http://www.atguigu.com">回娘家</a>
		<a href="http://www.atguigu.com">回娘家</a>
		<a href="http://www.atguigu.com">回娘家</a>
		<a href="http://www.atguigu.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>

event

在这里插入图片描述

<!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;
				bottom: 0;
				right: 0;
				margin: auto;
				width: 300px;
				height: 300px;
				background: pink;
			}
		</style>
	</head>
	<body>
		<div class="item"></div>
	</body>
	<script type="text/javascript">
		window.onload=function(){
			document.addEventListener("touchstart",function(ev){
				ev=ev||event;
				ev.preventDefault();
			})
			
			var item = document.querySelector(".item");
			//changedTouches;触发当前事件的手指列表
			//targetTouches:触发当前事件时元素上的手指列表
			//touches:触发当前事件时屏幕上的手指列表
			item.addEventListener("touchstart",function(ev){
				ev = ev||event;
				var item = document.querySelector(".item")
				item.innerHTML="changed:"+ev.changedTouches.length+"<br/>";
				item.innerHTML="target:"+ev.targetTouches.length+"<br/>";
				item.innerHTML="touches:"+ev.touches.length+"<br/>";
			})
		}
		
	</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯子@123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值