vue.js 3.2.20: 用rem实现移动端和pc的兼容

一,用vue实现浏览器兼容的目标:

1,      主要为了实现按照设计稿给出的宽度来设计页面,

       通常设计稿会给出750px或640px的宽度,

       我们在设置页面上元素宽度时要把象素转为rem,

       这样实现不管浏览器的宽度是多少,

       页面的表现要一致

       另外当pc端浏览时,把内容居中显示即可(这个视业务需求定,如pc页面显示和移动页面显示不一致有时会另开发一套前端)

2,   demo的地址:

https://gitee.com/liuhongdi/remdemo

说明:刘宏缔的架构森林是一个专注架构的博客,

网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/28/vue-js-3-2-20-yong-rem-shi-xian-yi-dong-duan-he-pc-de-jian/

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: 371125307@qq.com

 

二,编写代码:

1,main.js

import { createApp } from 'vue'
import App from './App.vue'

//判断是否移动端的函数
const isMobileFunc = () => {
    let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
    if (flag === null) {
        return 0;
    } else {
        return 1;
    }
};

//设置html的字号大小,作为rem的计算依据
const setHtmlFontSize = () => {
    let designWidth = 750;
    const htmlDom = document.getElementsByTagName('html')[0];
    let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth;
    if (isMobileFunc() === 1) {
        //移动端
        htmlDom.style.fontSize = `${htmlWidth / designWidth}px`;
    } else {
        //非移动端:
        //console.log('htmlWidth:'+htmlWidth);
        let dest = htmlWidth * 0.36;
        if (dest < 360) {
            dest = 360;
        }
        htmlDom.style.fontSize = `${dest / designWidth}px`;
    }
};

window.onresize = setHtmlFontSize;
setHtmlFontSize();

createApp(App).mount('#app')

 

2,App.vue

<template>

  <!--非移动端时显示-->
  <div v-if="ismobile == 0" style="width:100%;height:100vh;display: flex;justify-content: center;">
    <div style="width:36%;min-width:360px;height:100%;overflow: hidden;background: #ffff00;">
      <Home />
    </div>
  </div>
  <!--移动端时显示-->
  <Home v-if="ismobile == 1" />
</template>

<script>
import {ref} from "vue";
//import HelloWorld from './components/HelloWorld.vue'
import Home from './components/Home.vue'
export default {
  name: 'App',
  components: {
    Home
  },
  setup() {
    //是否移动端
    const ismobile = ref(0);
    //判断是否移动端的函数
    const isMobileFunc = () => {
      let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
      if (flag === null) {
        return 0;
      } else {
        return 1;
      }
    };
    if (isMobileFunc() === 1) {
      ismobile.value = 1;
    } else {
      ismobile.value = 0;
    }
    return {
      ismobile,
      isMobileFunc,
    };
  },
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  /*text-align: center;*/
  color: #2c3e50;
  /*margin-top: 60px;*/
}
body {
  display: block;
  margin: 0px;
}
</style>

 

3,Home.vue

<template>
<div style="width:100%;height:100%;">
  <div style="margin-top: 30rem;margin-left: 225rem;width:300rem;height:400rem;background: #ff0000;">
    <span style="font-size: 60rem;">首页</span><br/>
    <span style="font-size: 50rem;">首页</span><br/>
    <span style="font-size: 40rem;">首页</span><br/>
    <span style="font-size: 30rem;">首页</span><br/>
    <span style="font-size: 20rem;">首页</span><br/>
    <span style="font-size: 10rem;">首页</span>
  </div>
  <div style="margin-top: 30rem;width:750rem;height:486rem;background: #ff00ff;">
    <img src="@/assets/img/godzilla.jpg" style="width:700rem;margin-left:25rem;">
  </div>
</div>
</template>

<script>
export default {
  name: "Home"
}
</script>

<style scoped>

</style>

 

三,测试效果:

pc浏览器:

 

iphonex

 

 

ipad:

 

 

四,查看vue的版本:

liuhongdi@lhdpc:/data/vue/remdemo$ npm list vue
remdemo@0.1.0 /data/vue/remdemo
├─┬ @vue/cli-plugin-babel@4.5.13
│ └─┬ @vue/babel-preset-app@4.5.13
│   └── vue@3.2.20 deduped
└─┬ vue@3.2.20
  └─┬ @vue/server-renderer@3.2.20
    └── vue@3.2.20 deduped

 

posted @ 2021-10-11 12:44  刘宏缔的架构森林  阅读(942)  评论(0编辑  收藏  举报