- scrollingElement
该对象可以非常兼容地获取 scrollTop、scrollHeight 等属性,在移动端跟 PC 端都屡试不爽document.scrollingElement.scrollHeight标准模式返回 documentElement,怪异模式返回 body
- 滚动到底部
1
2
3
4
| window.scrollTo({
left: 0,
top: document.scrollingElement.scrollHeight,
});
|
- 判断浏览器已滚动到底部
1
2
3
4
5
6
7
| window.addEventListener("scroll", () => {
const { scrollTop, scrollHeight, clientHeight } = document.scrollingElement;
// 当前滚动高度 + 视口高度 >= 文档总高度
if (scrollTop + clientHeight >= scrollHeight) {
console.log("已到达底部");
}
});
|
- ios 设备局部滚动不顺畅
1
2
3
| .box {
-webkit-overflow-scrolling: touch;
}
|
- 滚动传播
指有多个滚动区域,当一个滚动区域滚动完之后,继续滚动会传播到到父区域继续滚动的行为
1
2
3
| .box {
overscroll-behavior: contain; // 阻止滚动传播
}
|
- 滚动结束后强制滚动到指定元素
1
2
3
4
5
6
| ul {
scroll-snap-type: x mandatory;
li {
scroll-snap-align: start;
}
}
|
- 移动端适配
1
2
3
| html {
font-size: 13.3333vw;
}
|
- input 的 placeholder 样式
input输入框的placeholder在移动端并非垂直居中 尤其不要尝试用height === line-height(这个会出现光标问题) 此时给input 添加属性line-height:normal可解决
9 滑动穿透
1
2
3
4
| document.querySelector(“.mask").addEventListener("touchmove", event => {
event.preventDefault();
})
在vue中也可以直接使用`@touchumove.prevent`
|
- ios 上 input 失焦后未回弹
1
2
3
4
5
6
| focus() {
this.scrollTop = document.scrollingElement.scrollTop
},
blur() {
document.scrollingElement.scrollTo(0, this.scrollTop)
}
|
- 网页忽略自动识别
1
2
3
4
| <!— 忽略浏览器自动识别数字为电话号码 —>
<meta name="format-detection" content="telephone=no" />
<!-- 忽略浏览器自动识别邮箱账号 -->
<meta name="format-detection" content="email=no" />
|
- 弹出数字键盘
1
2
3
4
5
6
7
8
9
10
11
12
13
| <!— 有”#" "*"符”输入 ——>
<input type="tel" novalidate="novalidate" pattern="[0-9]*" />
type="tel", 是 HTML5 的一个属性, 表示输入框类型为电话号码, 在 Android 和
IOS上表现差不多, 都会有数字键盘, 但是也会有字母, 略显多余 pattern="[0-9]",
pattern 用于验证表单输入的内容, 通常 HTML5 的 type 属性,
比如email、tel、number、data 类、url 等, 已经自带了简单的数据格式验证功能了,
加上pattern 后, 前端部分的验证更加简单高效了。IOS 中, 只有
[0-9]\*才可以调起九宫格数字键盘, \d 无效, Android 4.4以下(包括X5内核),
两者都调起数字键盘 novalidate="novalidate",
novalidate属性规定当提交表单时不对其进行验证, 由于 pattern校验兼容性不好,
可以不让其校验, 只让其唤起纯数字键盘, 校验工作由 js 去做
<!— 纯数字 -->
<input pattern="“\d*”" />
|
- 解决伪类失效
1
| <body ontouchstart></body>
|
- 网页状态栏背景色
1
2
3
4
| apple-mobile-web-app-capable是设置Web应用是否以全屏模式运行;语法:
<meta name="apple-mobile-web-app-capable" content="yes" /> //content设置为yes
Web应用会以全屏模式
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
|
- ios 键盘默认首字母大写解决方案
1
| <input autocapitalize=“off" autocorrect="off" />
|
- ios 上使用 input 但弹出自定义键盘
input 的 readonly 属性 在 ios 下 获取焦点时仍会弹出虚拟键盘 此时使用 unselectable=“on” onfocus=“this.blur()“解决
- 安卓上弹出的键盘覆盖文本框
安卓微信 H5 弹出软键盘后挡住 input 输入框 也可以在文本框获取焦点时不判断机型 直接加延时
1
2
3
4
5
6
7
8
9
10
| focus(){
let u = navigator.userAgent, app = navigator.appVersion;
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
if(isAndroid){
setTimeout(function() {
document.activeElement.scrollIntoViewIfNeeded();
document.activeElement.scrollIntoView();
}, 500);
}
}
|
- 锚点跳转
scrollIntoView()中可以传入 Boolean 类型的参数,默认是 true,就是滚动定位到浏览器的顶部,如果传入 false,就是滚动定位到浏览器的底部
1
| document.getElementById(IdSlector).scrollIntoView();
|
scrollIntoView 文档