前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mpvue适配iphone x

mpvue适配iphone x

作者头像
薛定喵君
发布2019-12-24 10:46:23
4180
发布2019-12-24 10:46:23
举报
文章被收录于专栏:薛定喵君薛定喵君薛定喵君

最近在学习mpvue,记一下苹果X安全区域的适配问题

# 判断机型工具类

safe-area.js

let cache = null
export default function getSafeArea() {
  return new Promise((resolve, reject) => {
    if (cache != null) {
      // 如果有缓存不行行调用
      resolve(cache)
    } else {
      // 获取系统信息
      wx.getSystemInfo({
        success: ({ model, screenHeight, statusBarHeight }) => {
          const iphoneX = /iphone x/i.test(model)
          const iphoneNew = /iPhone11/i.test(model) && screenHeight === 812
          cache = {
            isIPhoneX: iphoneX || iphoneNew,
            statusBarHeight,
          }
          resolve(cache)
        },
        fail: reject,
      })
    }
  })
}

# 全局组件mixin

# mixins.js

mounted过程中处理,避免不必要的注入

import getSafeArea from './utils/safe-area'
let MyPlugin = {}
MyPlugin.install = function(Vue) {
  // 添加全局方法或属性
  Vue.prototype.$isPage = function isPage() {
    return this.$mp && this.$mp.mpType === 'page'
  }
  // 注入组件
  Vue.mixin({
    data() {
      return {
        isIPhoneX: this.isIPhoneX,
      }
    },
    mounted() {
      if (this.$isPage()) {
        getSafeArea().then(({ isIPhoneX, statusBarHeight }) => {
          this.isIPhoneX = isIPhoneX
        })
      }
    },
  })
}
export default MyPlugin
# main.js中注册插件
import MyPlugin from './mixins'
Vue.use(MyPlugin)

# 安全距离css

.btn-iphonex {
  padding-bottom: 34px!important;
}

//吸底按钮
.submit-info {
  width: 100%;
  position: fixed;
  bottom: 0;
  &.safe {
    bottom: 34px;
  }
}

# 页面标签class类处理

项目中使用了iview库的情况,按钮标签为例

<i-button
  bind:click="submit"
  class="submit-info"
  :class="{safe:isIPhoneX}"
  type="primary"
  size="small"
>确定</i-button>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-12-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • # 判断机型工具类
  • # 全局组件mixin
    • # mixins.js
      • # main.js中注册插件
      • # 安全距离css
      • # 页面标签class类处理
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档