methods: {
device: function () { //判断手机类型
const u = navigator.userAgent; //用户代理,声明了浏览器用于 HTTP 请求的用户代理头的值 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if (isAndroid)
return 'android';
if (isiOS)
return 'ios';
},
},
mounted: function mounted() {
var that = this;
this.$nextTick(function () {
if (this.device() === 'ios') {
$("input").each(function () { //只能遍历jquery对象 指的是dom对象及input
$(this).blur(function () { //当元素失去焦点时发生 blur 事件。
window.scroll(0, 0); //设置滚动条滚动到顶部
})
})
}
})
},
created: function created() {
var useragent = navigator.userAgent; //通过userAgent是否包含MicroMessenger来判断是否在微信内置浏览器打开网页
if (useragent.match(/MicroMessenger/i) != "MicroMessenger") {
return false;
}
},
移动端设备类型的判断与响应式处理
本文介绍了一种在前端开发中判断用户设备类型(Android、iOS)的方法,并针对iOS设备进行了特定的响应式处理,如解决输入框聚焦导致的页面滚动问题。此外,还提供了检查是否在微信内置浏览器打开网页的代码片段。
1万+

被折叠的 条评论
为什么被折叠?



