1. 程式人生 > 其它 >vue.js 3.2.20: 用rem實現移動端和pc的相容

vue.js 3.2.20: 用rem實現移動端和pc的相容

一,用vue實現瀏覽器相容的目標:

1, 主要為了實現按照設計稿給出的寬度來設計頁面,

通常設計稿會給出750px或640px的寬度,

我們在設定頁面上元素寬度時要把象素轉為rem,

這樣實現不管瀏覽器的寬度是多少,

頁面的表現要一致

另外當pc端瀏覽時,把內容居中顯示即可(這個視業務需求定,如pc頁面顯示和移動頁面顯示不一致有時會另開發一套前端)

2, demo的地址:

https://gitee.com/liuhongdi/remdemo

說明:劉巨集締的架構森林是一個專注架構的部落格,地址:https://www.cnblogs.com/architectforest

對應的原始碼可以訪問這裡獲取:https://github.com/liuhongdi/
或:https://gitee.com/liuhongdi

說明:作者:劉巨集締 郵箱: [email protected]

二,編寫程式碼:

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