移动端H5开发常用技巧总结

html 篇

常用的meta属性设置

meta对于移动端的一些特殊属性,可根据需要自行设置

1<meta name="screen-orientation" content="portrait"> //Android 禁止屏幕旋转 2<meta name="full-screen" content="yes">             //全屏显示 3<meta name="browsermode" content="application">     //UC应用模式,使用了application这种应用模式后,页面讲默认全屏,禁止长按菜单,禁止收拾,标准排版,以及强制图片显示。 4<meta name="x5-orientation" content="portrait">     //QQ强制竖屏 5<meta name="x5-fullscreen" content="true">          //QQ强制全屏 6<meta name="x5-page-mode" content="app">            //QQ应用模式

电话号码识别

在 iOS Safari (其他浏览器和 Android 均不会)上会对那些看起来像是电话号码的数字处理为电话链接,比如:

  • 7 位数字,形如:1234567

  • 带括号及加号的数字,形如:(+86)123456789

  • 双连接线的数字,形如:00-00-00111

  • 11 位数字,形如:13800138000

关闭识别

<meta name="format-detection" content="telephone=no" />  

开启识别

<a href="tel:123456">123456</a>  

邮箱识别(Android)

安卓上会对符合邮箱格式的字符串进行识别,我们可以通过如下的 meta 来管别邮箱的自动识别:

<meta content="email=no" name="format-detection" />  

同样地,我们也可以通过标签属性来开启长按邮箱地址弹出邮件发送的功能:

<a mailto:dooyoe@gmail.com">dooyoe@gmail.com</a>  

css 篇

0.5px细线

移动端 H5 项目越来越多,设计师对于 UI 的要求也越来越高,比如 1px 的边框。在高清屏下,移动端的 1px 会很粗。

那么为什么会产生这个问题呢?主要是跟一个东西有关,DPR(devicePixelRatio) 设备像素比,它是默认缩放为 100%的情况下,设备像素和 CSS 像素的比值。目前主流的屏幕 DPR=2(iPhone 8),或者 3(iPhone 8 Plus)。拿 2 倍屏来说,设备的物理像素要实现 1 像素,而 DPR=2,所以 css 像素只能是 0.5。

下面介绍最常用的方法

1/* 底边框 */ 2.b-border { 3  position: relative; 4} 5.b-border:before { 6  content: ''; 7  position: absolute; 8  left: 0; 9  bottom: 0; 10  width: 100%; 11  height: 1px; 12  background: #d9d9d9; 13  -webkit-transform: scaleY(0.5); 14  transform: scaleY(0.5); 15  -webkit-transform-origin: 0 0; 16  transform-origin: 0 0; 17} 18/* 上边框 */ 19.t-border { 20  position: relative; 21} 22.t-border:before { 23  content: ''; 24  position: absolute; 25  left: 0; 26  top: 0; 27  width: 100%; 28  height: 1px; 29  background: #d9d9d9; 30  -webkit-transform: scaleY(0.5); 31  transform: scaleY(0.5); 32  -webkit-transform-origin: 0 0; 33  transform-origin: 0 0; 34} 35/* 右边框 */ 36.r-border { 37  position: relative; 38} 39.r-border:before { 40  content: ''; 41  position: absolute; 42  right: 0; 43  bottom: 0; 44  width: 1px; 45  height: 100%; 46  background: #d9d9d9; 47  -webkit-transform: scaleX(0.5); 48  transform: scaleX(0.5); 49  -webkit-transform-origin: 0 0; 50  transform-origin: 0 0; 51} 52/* 左边框 */ 53.l-border { 54  position: relative; 55} 56.l-border:before { 57  content: ''; 58  position: absolute; 59  left: 0; 60  bottom: 0; 61  width: 1px; 62  height: 100%; 63  background: #d9d9d9; 64  -webkit-transform: scaleX(0.5); 65  transform: scaleX(0.5); 66  -webkit-transform-origin: 0 0; 67  transform-origin: 0 0; 68} 69 70/* 四条边 */ 71.setBorderAll { 72  position: relative; 73  &:after { 74    content: ' '; 75    position: absolute; 76    top: 0; 77    left: 0; 78    width: 200%; 79    height: 200%; 80    transform: scale(0.5); 81    transform-origin: left top; 82    box-sizing: border-box; 83    border: 1px solid #e5e5e5; 84    border-radius: 4px; 85  } 86}

屏蔽用户选择

禁止用户选择页面中的文字或者图片

1div { 2  -webkit-touch-callout: none; 3  -webkit-user-select: none; 4  -khtml-user-select: none; 5  -moz-user-select: none; 6  -ms-user-select: none; 7  user-select: none; 8}

清除输入框内阴影

在 iOS 上,输入框默认有内部阴影,以这样关闭:

1div { 2  -webkit-appearance: none; 3}

如何禁止保存或拷贝图像

代码如下

1img { 2  -webkit-touch-callout: none; 3}

输入框默认字体颜色

设置 input 里面 placeholder 字体的颜色

1input::-webkit-input-placeholder, 2textarea::-webkit-input-placeholder { 3  color: #c7c7c7; 4} 5input:-moz-placeholder, 6textarea:-moz-placeholder { 7  color: #c7c7c7; 8} 9input:-ms-input-placeholder, 10textarea:-ms-input-placeholder { 11  color: #c7c7c7; 12}

用户设置字号放大或者缩小导致页面布局错误

设置字体禁止缩放

1body { 2  -webkit-text-size-adjust: 100% !important; 3  text-size-adjust: 100% !important; 4  -moz-text-size-adjust: 100% !important; 5}

android系统中元素被点击时产生边框

部分android系统点击一个链接,会出现一个边框或者半透明灰色遮罩, 不同生产商定义出来额效果不一样。去除代码如下

1a,button,input,textarea{ 2  -webkit-tap-highlight-color: rgba(0,0,0,0) 3  -webkit-user-modify:read-write-plaintext-only;  4}

iOS 滑动不流畅

ios 手机上下滑动页面会产生卡顿,手指离开页面,页面立即停止运动。整体表现就是滑动不流畅,没有滑动惯性。iOS 5.0 以及之后的版本,滑动有定义有两个值 auto 和 touch,默认值为 auto。

「解决方案」

  1. 在滚动容器上增加滚动 touch 方法
1.wrapper { 2  -webkit-overflow-scrolling: touch; 3}
  1. 设置 overflow 设置外部 overflow 为 hidden,设置内容元素 overflow 为 auto。内部元素超出 body 即产生滚动,超出的部分 body 隐藏。
1body { 2  overflow-y: hidden; 3} 4.wrapper { 5  overflow-y: auto; 6}

js 篇

移动端click屏幕产生200-300 ms的延迟响应

移动设备上的web网页是有300ms延迟的,往往会造成按钮点击延迟甚至是点击失效。解决方案:

  • fastclick可以解决在手机上点击事件的300ms延迟

  • zepto的touch模块,tap事件也是为了解决在click的延迟问题

触摸事件的响应顺序

  1. ontouchstart

  2. ontouchmove

  3. ontouchend

  4. onclick

audio 和 video 在 ios 和 andriod 中自动播放

这个不是bug,由于自动播放网页中的音频或视频,会给用户带来一些困扰或者不必要的流量消耗,所以苹果系统和安卓系统通常都会禁止自动播放和使用 JS 的触发播放,必须由用户来触发才可以播放。加入自动触发播放的代码

1$('html').one('touchstart', function() { 2  audio.play() 3})

iOS 上拉边界下拉出现空白

手指按住屏幕下拉,屏幕顶部会多出一块白色区域。手指按住屏幕上拉,底部多出一块白色区域。

在 iOS 中,手指按住屏幕上下拖动,会触发 touchmove 事件。这个事件触发的对象是整个 webview 容器,容器自然会被拖动,剩下的部分会成空白。

「解决方案」

1document.body.addEventListener( 2  'touchmove', 3  function(e) { 4    if (e._isScroller) return 5    // 阻止默认事件 6    e.preventDefault() 7  }, 8  { 9    passive: false 10  } 11)

ios 日期转换 NAN 的问题

将日期字符串的格式符号替换成'/'

'yyyy-MM-dd'.replace(/-/g, '/')  

软键盘问题

IOS 键盘弹起挡住原来的视图

  • 可以通过监听移动端软键盘弹起 Element.scrollIntoViewIfNeeded(Boolean)方法用来将不在浏览器窗口的可见区域内的元素滚动到浏览器窗口的可见区域。如果该元素已经在浏览器窗口的可见区域内,则不会发生滚动。

  • true,则元素将在其所在滚动区的可视区域中居中对齐。

  • false,则元素将与其所在滚动区的可视区域最近的边缘对齐。根据可见区域最靠近元素的哪个边缘,元素的顶部将与可见区域的顶部边缘对准,或者元素的底部边缘将与可见区域的底部边缘对准。

1window.addEventListener('resize', function() { 2  if ( 3    document.activeElement.tagName === 'INPUT' || 4    document.activeElement.tagName === 'TEXTAREA' 5  ) { 6    window.setTimeout(function() { 7      if ('scrollIntoView' in document.activeElement) { 8        document.activeElement.scrollIntoView(false) 9      } else { 10        document.activeElement.scrollIntoViewIfNeeded(false) 11      } 12    }, 0) 13  } 14})

onkeyUp 和 onKeydown 兼容性问题

IOS 中 input 键盘事件 keyup、keydown、等支持不是很好, 用 input 监听键盘 keyup 事件,在安卓手机浏览器中没有问题,但是在 ios 手机浏览器中用输入法输入之后,并未立刻相应 keyup 事件

IOS12 输入框难以点击获取焦点,弹不出软键盘

定位找到问题是 fastclick.js 对 IOS12 的兼容性,可在 fastclick.js 源码或者 main.js 做以下修改

1FastClick.prototype.focus = function(targetElement) { 2  var length 3  if ( 4    deviceIsIOS && 5    targetElement.setSelectionRange && 6    targetElement.type.indexOf('date') !== 0 && 7    targetElement.type !== 'time' && 8    targetElement.type !== 'month' 9  ) { 10    length = targetElement.value.length 11    targetElement.setSelectionRange(length, length) 12    targetElement.focus() 13  } else { 14    targetElement.focus() 15  } 16}

IOS 键盘收起时页面没用回落,底部会留白

通过监听键盘回落时间滚动到原来的位置

1window.addEventListener('focusout', function() { 2  window.scrollTo(0, 0) 3}) 4 5//input输入框弹起软键盘的解决方案。 6var bfscrolltop = document.body.scrollTop 7$('input') 8  .focus(function() { 9    document.body.scrollTop = document.body.scrollHeight 10    //console.log(document.body.scrollTop); 11  }) 12  .blur(function() { 13    document.body.scrollTop = bfscrolltop 14    //console.log(document.body.scrollTop); 15  })

IOS 下 fixed 失效的原因

软键盘唤起后,页面的 fixed 元素将失效,变成了 absolute,所以当页面超过一屏且滚动时,失效的 fixed 元素就会跟随滚动了。不仅限于 type=text 的输入框,凡是软键盘(比如时间日期选择、select 选择等等)被唤起,都会遇到同样地问题。

解决方法: 不让页面滚动,而是让主体部分自己滚动,主体部分高度设为 100%,overflow:scroll

1<body> 2  <div class='warper'> 3    <div class='main'></div> 4  <div> 5  <div class="fix-bottom"></div> 6</body> 7.warper { 8  position: absolute; 9  width: 100%; 10  left: 0; 11  right: 0; 12  top: 0; 13  bottom: 0; 14  overflow-y: scroll; 15  -webkit-overflow-scrolling: touch; /* 解决ios滑动不流畅问题 */ 16} 17.fix-bottom { 18  position: fixed; 19  bottom: 0; 20  width: 100%; 21}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )