前言
开发移动端最重要的就是适配各种手机,在之前我们用的是rem根据HTML font-size去做缩放,在此探讨另一种解决方案vw,vh
-
vw 视口的最大宽度,1vw等于视口宽度的百分之一
-
vh 视口的最大高度,1vh等于视口高度的百分之一
安装依赖
npm install postcss-px-to-viewport -D
脚手架使用的vite,因为vite中已经内联了postcss,所以并不需要额外的创建 postcss.config.js文件
vite.config.ts
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// px转vw,vh
import pxtoViewPort from 'postcss-px-to-viewport'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
css: {
postcss: {
plugins: [
pxtoViewPort({
unitToConvert: 'px', // 要转化的单位
viewportWidth: 320 // UI设计稿的宽度
})
]
}
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
}
})
****
如果你用的vite 是 ts 他这个插件并没有提供声明文件我已经帮大家写好了声明文件
declare module 'postcss-px-to-viewport' {
type Options = {
unitToConvert: 'px' | 'rem' | 'cm' | 'em', // 要转化的单位
viewportWidth: number, // UI设计稿的宽度
viewportHeight: number, // not now used; TODO: need for different units and math for different properties
unitPrecision: number, // 转换后的精度,即小数点位数
viewportUnit: string, // 指定需要转换成的视窗单位,默认vw
fontViewportUnit: string, // vmin is more suitable. // 指定字体需要转换成的视窗单位,默认vw
selectorBlackList: string[], // 指定不转换为视窗单位的类名
propList: string[], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换
minPixelValue: number, // 默认值1,小于或等于1px则不进行转换
mediaQuery: boolean, // 是否在媒体查询的css代码中也进行转换,默认false
replace: boolean, // 是否转换后直接更换属性值
landscape: boolean, // 是否处理横屏情况
landscapeUnit: string,
landscapeWidth: number
}
// Partial 非必填
export default function (options: Partial<Options>): any
}
引入声明文件 tsconfig.app.json postcss-px-to-viewport.d.ts跟vite.ts同级
{
"extends": "@vue/tsconfig/tsconfig.node.json",
"include": ["vite.config.*", "vitest.config.*", "cypress.config.*", "postcss-px-to-viewport.*"],
"compilerOptions": {
"composite": true,
"types": ["node"]
}
}
代码案例
<template>
<div class="wraps">
<header class="header">
<div>left</div>
<div>中间</div>
<div>right</div>
</header>
<main class="main">
<div class="main-items" v-for="item in 100">
<div class="main-port">头像</div>
<div class="main-desc">
<div>zs{{item}}</div>
<div>你妈喊你回家吃饭</div>
</div>
</div>
</main>
<footer class="footer">
<div class="footer-items" v-for="item in footer">
<div>{{ item.icon }}</div>
<div>{{ item.text }}</div>
</div>
</footer>
</div>
</template>
<script setup lang='ts'>
import { reactive } from 'vue';
type Footer<T> = {
icon: T,
text: T
}
const footer = reactive<Footer<string>[]>([
{
icon: "1",
text: "首页"
},
{
icon: "2",
text: "商品"
},
{
icon: "3",
text: "信息"
},
{
icon: "4",
text: "我的"
}
])
</script>
<style lang="less">
@import url('@/assets/base.css');
html,
body,
#app {
height: 100%;
overflow: hidden;
font-size: 14px;
}
.wraps {
height: inherit;
overflow: hidden;
display: flex;
flex-direction: column;
}
.header {
background-color: pink;
display: flex;
height: 30px;
align-items: center;
justify-content: space-around;
div:nth-child(1) {
width: 40px;
}
div:nth-child(2) {
text-align: center;
}
div:nth-child(3) {
width: 40px;
text-align: right;
}
}
.main {
flex: 1;
overflow: auto;
&-items {
display: flex;
border-bottom: 1px solid #ccc;
box-sizing: border-box;
padding: 5px;
}
&-port {
background: black;
width: 30px;
height: 30px;
border-radius: 200px;
}
&-desc{
margin-left:10px;
div:last-child{
font-size: 10px;
color:#333;
margin-top: 5px;
}
}
}
.footer {
border-top: 1px solid #ccc;
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
&-items {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 5px;
}
}
</style>
效果
bak: 学习来自B站小满zs