200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > [移动端开发]H5(安卓/iOS)移动端开发技巧

[移动端开发]H5(安卓/iOS)移动端开发技巧

时间:2021-05-28 10:41:26

相关推荐

[移动端开发]H5(安卓/iOS)移动端开发技巧

转载自微信公众号:前端进阶周刊 文章《你还会再爱一次的H5移动端开发技巧》,侵删

1. 弹出数字键盘

<!-- 有"#" "*"符号输入 --><input type="tel"><!-- 纯数字 --><input pattern="\d*">

安卓跟IOS的表现形式应该不一样,大家可以自己试试。当运用了正则pattern后,就不用关注input的类型了

2. 调用系统的某些功能

<!-- 拨号 --><a href="tel:10086">打电话给: 10086</a><!-- 发送短信 --><a href="sms:10086">发短信给: 10086</a><!-- 发送邮件 --><a href="mailto:839626987@">发邮件给:839626987@</a><!-- 选择照片或者拍摄照片 --><input type="file" accept="image/*"><!-- 选择视频或者拍摄视频 --><input type="file" accept="video/*"><!-- 多选 --><input type="file" multiple>

3. 打开原生应用

<a href="weixin://">打开微信</a><a href="alipays://">打开支付宝</a><a href="alipays://platformapi/startapp?saId=10000007">打开支付宝的扫一扫功能</a><a href="alipays://platformapi/startapp?appId=60000002">打开支付宝的蚂蚁森林</a>

这种方式叫做URL Scheme,是一种协议,一般用来访问APP或者APP中的某个功能/页面(如唤醒APP后打开指定页面或者使用某些功能)

URL Scheme的基本格式如下:

行为(应用的某个功能/页面) |scheme://[path][?query]| 应用标识 |功能需要的参数

一般是由APP开发者自己定义,比如规定一些参数或者路径让其他开发者来访问,就像上面的例子

注意事项:

唤醒APP的条件是你的手机已经安装了该APP某些浏览器会禁用此协议,比如微信内部浏览器(除非开了白名单)

4. 解决active伪类失效,给body注册一个空事件即可

<body ontouchstart></body>

5. 忽略自动识别

<!-- 忽略浏览器自动识别数字为电话号码 --><meta name="format-detection" content="telephone=no"><!-- 忽略浏览器自动识别邮箱账号 --><meta name="format-detection" content="email=no">

当页面上的内容包含了手机号/邮箱等,会自动转换成可点击的链接😁

比如你有如下代码:

<p>13192733603</P>

但是有些浏览器会识别为手机,并且可以点击拨号。

6. 解决input失焦后页面没有回弹

一般出现在IOS设备中的微信内部浏览器,出现的条件为:

页面高度过小聚焦时,页面需要往上移动的时候

所以一般input在页面上方或者顶部都不会出现无法回弹🤣

解决办法为,在聚焦时,获取当前滚动条高度,然后失焦时,赋值之前获取的高度:

<template><input type="text" @focus="focus" @blur="blur"></template><script>export default {data() {return {scrollTop: 0}},methods: {focus() {this.scrollTop = document.scrollingElement.scrollTop;},blur() {document.scrollingElement.scrollTo(0, this.scrollTop);}}}</script>

7. 禁止长按

长按图片保存、长按选择文字、长按链接/手机号/邮箱时呼出菜单。

想要禁止这些浏览器的默认行为,可以使用以下CSS:

// 禁止长按图片保存img {-webkit-touch-callout: none;pointer-events: none; // 像微信浏览器还是无法禁止,加上这行样式即可}// 禁止长按选择文字div {-webkit-user-select: none;}// 禁止长按呼出菜单div {-webkit-touch-callout: none;}

8. 滑动不顺畅,粘手

一般出现在IOS设备中,自定义盒子使用了overflow: auto || scroll后出现的情况。

div {-webkit-overflow-scrolling: touch;}

屏幕旋转为横屏时,字体大小会变

具体出现的情况不明😒,有时候有有时候没有,欢迎指出。

优化代码:

* {-webkit-text-size-adjust: 100%;}

10. 最简单的rem自适应

大家都知道,rem的值是根据根元素的字体大小相对计算的,但是我们每个设备的大小不一样,所以根元素的字体大小要动态设置

html {font-size: calc(100vw / 3.75);}body {font-size: .14rem;}

像我一般的话,直接搞lib-flexible、postcss-pxtorem就完事啦!

11. 滑动穿透

当你想在出现遮罩的时候,锁住用户的滚动行为,你可以这么做。

假设HTML结构如下:

<div class="mask"><div class="content">我是弹框</div></div>CSS样式如下:.mask {position: fixed;top: 0;left: 0;display: flex;justify-content: center;align-items: center;width: 100%;height: 100%;background-color: rgba($color: #333, $alpha: .6);.content {padding: 20px;background-color: #fff;width: 300px;}}

效果如下:

可以看到,当在遮罩上滑动的时候,是会穿透到父节点的,最简单的办法就是阻住默认行为:

document.querySelector(".mask").addEventListener("touchmove", event => {event.preventDefault();});

如果在vue中,你可以这么写:

<div class="mask" @touchumove.prevent></div>

如果.content也有滚动条,那么只要阻止遮罩本身就行:

document.querySelector(".mask").addEventListener("touchmove", event => {if (event.target.classList.contains("mask")) event.preventDefault();});

或者:

<div class="mask" @touchumove.self.prevent></div>

这样,当出现遮罩的时候用户的滑动就会被锁住啦

12. CSS3 text-overflow 属性,一行文字显示不完时,显示。。。

使用 text-overflow 属性:

.test{text-overflow:ellipsis;}

转载自 https://mp./s/CfEHh4DLDDk4hOv075q3Lg

13. 将ios的的默认样式清除

-webkit-appearance: none;

单数会出现将raido的选择按钮也会消失 所以需要对radio的样式进行重新设置

设置input框的样式

input[type="text"]{-webkit-appearance: none; font-size: 12px;}

设置radio选择之前和之后的样式

input[type="radio"]:checked , input[type="checkbox"]:checked {-webkit-appearance: none; border-color: #f67200;background-color: #f67200;}input[type="radio"], input[type="checkbox"] {-webkit-appearance: none;position: relative;width: 14px;height: 14px;border: 1px solid #e6e6e6;-webkit-border-radius: 7px;border-radius: 7px;background-color: #fff;vertical-align: middle;}input[type="checkbox"] {-webkit-appearance: none;position: relative;width: 14px;height: 14px;border: 1px solid #e6e6e6;-webkit-border-radius: 2px;border-radius: 2px;background-color: #fff;vertical-align: middle;}

设置选择之后checkbox的样式

input[type="checkbox"]:checked:after {content: '';position: absolute;left: 1px;top: 1px;width: 10px;height: 5px;border-left: 2px solid #fff;border-bottom: 2px solid #fff;-webkit-transform: rotate(-45deg);-ms-transform: rotate(-45deg);transform: rotate(-45deg); }

css常用样式集合

文字强制换行:word-wrap:word-break; word-break:break-all;

强制不换行:white-space:nowrap;

iphone及ipad下输入框默认内阴影:-webkit-appearance: none;

在360浏览器中input有黄色背景解决办法: 在input中加入autocomplete=“off”即可。

防止按钮点击时背景闪烁:html{ -webkit-tap-highlight-color: transparent; }

去除移动端下按钮点击背景变暗:-webkit-tap-highlight-color:rgba(0,0,0,0)

去除输入框黄色边框:outline:none;

改变斜体:font-style:normal;

某些安卓手机圆角失效:background-clip: padding-box;

删除线样式-贯穿线样式 :text-decoration:line-through

手型:cursor:pointer;

字体间距:

letter-spacing: 2px;

textarea 在浏览器中固定大小和禁止拖动:

resize: none;

动画效果无限播放:animation-iteration-count:infinite; -webkit-animation-iteration-count:infinite;

IOS中防止复制和保存图片:touch-callout:none;

禁止长按链接与图片弹出菜单:img{ -webkit-touch-callout: none; }

android 上去掉语音输入按钮:input::-webkit-input-speech-button { display: none }

流畅滚动:body{ -webkit-overflow-scrolling:touch; overflow-scrolling: touch; }

禁用iPhone中Safari的字号自动调整:html{ -webkit-text-size-adjust: none; }

css3背景渐变:background: -moz-linear-gradient(top, #ededf0,#fcfcfc,#fdfdfd ,#f4f4f6,#eaeaee);

输入框默认显示文字: placeholder=“请输文字” IE8不支持

解决placeholder会出现文本位置偏上的情况: line-height:normal

改变表单输入框placeholder的颜色值:

input::-webkit-input-placeholder{color:#AAAAAA;}input:focus::-webkit-input-placeholder{color:#EEEEEE;}

去除 a/button/input标签被点击时产生的边框,

去除ios a标签被点击时产生的半透明灰色背景:

a,button,input{-webkit-tap-highlight-color:rgba(255,0,0,0);}

文字两端对齐:

text-align:justify;text-justify:distribute-all-lines;/ie6-8/text-align-last:justify;/* ie9*/-moz-text-align-last:justify;/ff/-webkit-text-align-last:justify;/chrome 20+/

背景图片做成视觉滚差:

background:url(…/images/bg_1.jpg) no-repeat center center / cover;background-color: transparent;background-attachment: fixed;position: relative; overflow:hidden

分栏布局,给父元素定义:

display: flex;display: -webkit-box;display:-moz-box;display:-webkit-flex;display:-ms-flexbox;

分栏布局,给子元素定义:

-webkit-box-flex:3.5;-webkit-flex:3.5;-ms-flex:3.5;flex:3.5;

分栏布局,垂直居中:

flex-direction:column;display: flex;display: -webkit-flex;justify-content: center;-webkit-justify-content: center;align-items:center;-webkit-align-items: center;

超出2行,文字省略:

overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 2;

文字溢出隐藏,省略:

overflow: hidden;white-space: nowrap;text-overflow: ellipsis; -o-text-overflow: ellipsis;-icab-text-overflow: ellipsis;-khtml-text-overflow: ellipsis;-moz-text-overflow: ellipsis;-webkit-text-overflow: ellipsis;

背景半透明:

background:rgba(0,0,0,0.2) none repeat scroll !important; /实现FF背景透明,文字不透明/background:#000;filter:Alpha(opacity=20);

fit-content是CSS3中给width属性新加的一个属性值,它配合margin可以让我们轻松的实现水平居中的效果;

width:-moz-fit-content;width:-webkit-fit-content;width:fit-content;margin:auto

目前只支持谷歌和火狐浏览器。

禁止输入框输入空格: οnkeyup=“this.value=this.value.replace(/\s+/g,’’)”

Vue实现vw自适应

Vue实现vw自适应

网易三年实践大佬

中高级前端必须注意的40条移动端H5坑位指南-HTML方向

中高级前端必须注意的40条移动端H5坑位指南 | 网易三年实践

H5项目中踩过的坑及解决办法

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。