vue前端页面自适应解决方案

什么是自适应?

定义

自适应是一套代码能够自动适配各个终端。一次设计,普遍适用。

自适应方案

使用百分比
<style>
  .box {
    width: 100vw;
    height: 100vh;
    .inner-box {
      width: 20%;
      height: 20%;
      background: cadetblue;
    }
  }
</style>
使用rem,resize

rem的原理是根据父组件的fontsize来计算rem的单位长度。可以通过监听window的resize,动态修改fontsize,进而影响rem,最终达到自适应的效果
resize.js

const resize=()=>{
    //与原来1080的比值
    let scale=window.innerHeight/1080;
    document.documentElement.style.fontSize=`${32*scale}px`;
    console.log('resize');
}
const scaleListener=()=>{
    window.addEventListener('resize',resize);
    console.log('scaleListening');
}
export {
    scaleListener
}

Remandresizeselfadaption.vue

<template>
    <div class="remandresizeselfadaption">
        内容提示
    </div>
</template>
<script>
import { scaleListener } from './resize';
export default{
    mounted(){
        scaleListener();
    }
}
   
</script>

媒体查询语句
@media screen and (max-width:300px){
   html{
     width:300px;
     font-size:12px;
   }
}
@media screen and (max-width:500px){
     html{
     width:500px;
     font-size:20px;
     margin:0 auto; 
   }
}
flex布局
<template>
    <div class="home">
        <img src="../assets/vue.svg" alt="Vue logo">
        <div class="flex-test">
            <div class="div1">图片1</div>
            <div class="div2">图片2</div>
            <div class="div3">图片3</div>
            <div class="div4">图片4</div>
        </div>
    </div>
</template>
<script>
export default{

}
</script>
<style scoped>
.flex-test {
    display: flex;
    flex-direction: row;
    width: 60vw;
    height: 50vh;
 
}
.div1 {
        width: 20%;
        background-color: cadetblue;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .div2 {
        width: 20%;
        background-color: darkgray;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .div3 {
        width: 20%;
        background-color: darkolivegreen;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .div4 {
        width: 20%;
        background-color: green;
        display: flex;
        align-items: center;
        justify-content: center;
    }
</style>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值