1、css修改input、textarea标签placeholder属性默认文字颜色
input::-webkit-input-placeholder {
color: #9e9e9e;
}
input:-moz-placeholder {
color: #9e9e9e;
}
input::-moz-placeholder {
color: #9e9e9e;
}
input::-ms-input-placeholder {
color: #9e9e9e;
}
2、正则判断是否为手机号
isPoneAvailable (pone) {
var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
if (!myreg.test(pone)) {
return false;
} else {
return true;
}
}
3、获取页面url参数
function getQuery(key) {
let query = location.href.split('?')[1]
if(!query) {
return
}
query = query.split('&')
for(let i in query) {
let q = query[i].split('=')
if(q[0] == key) {
return q[1]
}
}
}
4、解决移动端滑动穿透
export function setHtmlScroll(v, flag) {
if(flag) {
v.curPos = document.documentElement.scrollTop || document.body.scrollTop;
document.querySelector('html').style.position = 'fixed';
document.querySelector('html').style.top = -v.curPos + 'px';
}
if(!flag) {
document.querySelector('html').style.position = 'static';
document.querySelector('html').style.top = 'auto';
window.scrollTo(0, v.curPos);
}
}
export default {
data() {
return {
curPos: 0,
};
},
}
5、正则表达式校验只能输入数字、保留两位小数、不可为0、不可为负
//取数字
newVal = newVal.replace(/[^\d.]/g,"");
//保证只有出现一个.而没有多个.
newVal = newVal.replace(/\.{2,}/g,".");
//必须保证第一个为数字而不是.
newVal = newVal.replace(/^\./g,"");
//保证.只出现一次,而不能出现两次以上
newVal = newVal.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
//只能输入两个小数
newVal = newVal.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
6、element-ui datePicker 设置当前日期之前的日期不可选
<template>
<el-date-picker
v-model="value2"
align="right"
type="date"
placeholder="选择日期"
:picker-options="pickerOptions1">
</el-date-picker>
</template>
<script>
export default {
data() {
return {
pickerOptions1: {
disabledDate(time) {
return time.getTime() < Date.now() - 8.64e7;
},
}
}
}
}
7、设置当前日期之后的日期不可选
disabledDate(time) {
return time.getTime() > Date.now();
}
8、弹出数字键盘
<!-- 有"#" "*"符号输入 -->
<input type="tel">
<!-- 纯数字 -->
<input pattern="\d*">
9、用系统的某些功能
<!-- 拨号 -->
<a href="tel:10086">打电话给: 10086</a>
<!-- 发送短信 -->
<a href="sms:10086">发短信给: 10086</a>
<!-- 发送邮件 -->
<a href="mailto:839626987@qq.com">发邮件给:839626987@qq.com</a>
<!-- 选择照片或者拍摄照片 -->
<input type="file" accept="image/*">
<!-- 选择视频或者拍摄视频 -->
<input type="file" accept="video/*">
<!-- 多选 -->
<input type="file" multiple>
10、打开原生应用
<a href="weixin://">打开微信</a>
<a href="alipays://">打开支付宝</a>
<a href="alipays://platformapi/startapp?saId=10000007">打开支付宝的扫一扫功能</a>
<a href="alipays://platformapi/startapp?appId=60000002">打开支付宝的蚂蚁森林</a>
11、忽略自动识别
<!-- 忽略浏览器自动识别数字为电话号码 -->
<meta name="format-detection" content="telephone=no">
<!-- 忽略浏览器自动识别邮箱账号 -->
<meta name="format-detection" content="email=no">
12、解决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>
13、禁止长按
// 禁止长按图片保存
img {
-webkit-touch-callout: none;
pointer-events: none; // 像微信浏览器还是无法禁止,加上这行样式即可
}
// 禁止长按选择文字
div {
-webkit-user-select: none;
}
// 禁止长按呼出菜单
div {
-webkit-touch-callout: none;
}
14、滑动不顺畅,粘手
一般出现在IOS设备中,自定义盒子使用了overflow: auto || scroll后出现的情况。
div {
-webkit-overflow-scrolling: touch;
}
15、屏幕旋转为横屏时,字体大小会变
* {
-webkit-text-size-adjust: 100%;
}
16、最简单的rem自适应
// 初始化
html {
font-size: calc(100vw / 3.75);
}
// 子元素使用
body {
font-size: .14rem;
}
17、滑动穿透
当在遮罩上滑动的时候,是会穿透到父节点的,最简单的办法就是阻住默认行为:
<div class="mask">
<div class="content">我是弹框</div>
</div>
.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>
这样,当出现遮罩的时候用户的滑动就会被锁住啦👌
网友评论