背景
在公司参与一个原生APP和h5混合开发的项目,本人在项目中负责h5部分,现将项目中遇到的问题总结如下:
具体问题
问题1:页面滚动条问题
问题描述
web页面在PC浏览器上浏览时有滚动条;但是,在移动端浏览器打开时,没有滚动条
解决方法
将页面的最外层(我一般在写页面时,会在body标签内写一个大容器,用于存放页面的内容)设置overflow:auto/scroll;并且不能设置height属性的值(height:100%也不行)
例子
| <body> <div style="overflow:scroll/auto;"> //网页内容 </div> </body> |
问题2:touchstart 和 touchend 事件的使用
问题描述
引入touch.js文件,使用touchstart和touchend事件实现交互效果时,在部分手机出现事件触发失效的问题[例如:低版本的荣耀手机]
解决方法
方法1:"removeEventListener"和"addEventListener"一起使用
方法2:添加e.preventDefault(); 阻止部分手机默认跳转
法3:Jquery的on实现事件绑定
说明:法1与法2都是原生JS使用addEventListener实现事件监听;并且dom元素使用touchstart和touchend事件时,需要结合事件绑定或者事件监听一起使用,否则js部分会抛出异常
代码
| //法一: document.getElementById('list5').addEventListener('touchstart',callback, false); document.getElementById('list5').removeEventListener('touchstart',callback, false); document.getElementById('list5').addEventListener('touchend',callback, false); document.getElementById('list5').removeEventListener('touchend',callback, false); //法二: document.getElementById('list5').addEventListener('touchstart',function(e){ e.preventDefault(); }, false); document.getElementById('list5').addEventListener('touchend',function(e){ e.preventDefault(); }, false); |
问题3:长按闪退的问题
情景还原
有一个XXX列表页,长按列表页的列表项时(触摸到文字),在低版本手机中会出现闪退的情况
解决方法
js部分:在事件触发时添加e.preventDefault();,用于阻止默认行为
css部分:添加禁止文本文本复制的代码
代码
| //js部分: e.preventDefault(); //css部分: -webkit-touch-callout: none; //解决闪退 //禁止复制 -moz-user-select: none; -khtml-user-select: none; user-select: none; |
问题4: 移动端1px的问题
问题描述
由于不同的手机有不同的像素密度,css中的1px并不等于移动设备的1px。项目中使用js和rem做移动端的屏幕适配,所以产生0.5px的情况,导致低版本的手机展示不了0.5px的边框。
解决方法
使用css解决1px的问题,并且给需要设置成1px的dom元素直接写上:border-width:1px;









