美文网首页
前端大屏之vw vh

前端大屏之vw vh

作者: Hasan_hs | 来源:发表于2023-08-03 16:07 被阅读0次

    当接到可视化大屏需求时,你是否会有以下疑问👇如何做一款定制化的数据大屏? 开发可视化数据大屏如何做自适应? vm vh、rem、scale 到底哪种比较好? 时间不够,有没有偷懒的方法?

    实现方法:

    css 方案 - sass

    utils.scss

    // 使用 scss 的 math 函数,https://sass-lang.com/documentation/breaking-changes/slash-div
    @use "sass:math";
     
    // 默认设计稿的宽度
    $designWidth: 1920;
    // 默认设计稿的高度
    $designHeight: 1080;
     
    // px 转为 vw 的函数
    @function vw($px) {
      @return math.div($px, $designWidth) * 100vw;
    }       
     
    // px 转为 vh 的函数
    @function vh($px) {
      @return math.div($px, $designHeight) * 100vh;
    }
    
    

    路径配置只需在vue.config.js里配置一下utils.scss的路径,就可以全局使用了
    vue.config.js

    module.exports = {
        css: {
            loaderOptions: {
                scss: {
                    prependData: `@import "@/assets/css/utils.scss";`
                }
            }
        },
    }
    //如果node版本比较高的话,api有会有变化
    const { defineConfig } = require('@vue/cli-service')
    module.exports = defineConfig({
      transpileDependencies: true,
      css: {
        loaderOptions: {
            scss: {
                additionalData: `@import "@/scss/utils.scss";`
            }
        }
    }
      
    })
    
    

    在 .vue 中使用

    <template>
        <div class="box">   
        </div>
    </template>
     
    <script>
    export default{
        name: "Box",
    }
    </script>
     
    <style lang="scss" scoped="scoped">
    /* 
     直接使用 vw 和 vh 函数,将像素值传进去,得到的就是具体的 vw vh 单位   
     */
    .box{
        width: vw(300);
        height: vh(100);
        font-size: vh(16);
        background-color: black;
        margin-left: vw(10);
        margin-top: vh(10);
        border: vh(2) solid red;
    }
    </style>
    
    

    css 方案 - less

    utils.less
    @charset "utf-8";
    // 默认设计稿的宽度
    @designWidth: 1920;
    // 默认设计稿的高度
    @designHeight: 1080;
     
    .px2vw(@name, @px) {
      @{name}: (@px / @designWidth) * 100vw;
    }
     
    .px2vh(@name, @px) {
      @{name}: (@px / @designHeight) * 100vh;
    }
     
    .px2font(@px) {
      font-size: (@px / @designWidth) * 100vw;
    }
    
    路径配置在vue.config.js里配置一下utils.less
    <style lang="less" scoped="scoped">
    /* 
     直接使用 vw 和 vh 函数,将像素值传进去,得到的就是具体的 vw vh单位   
     */
    .box{
        .px2vw(width, 300);
        .px2vh(height, 100);
        .px2font(16);
        .px2vw(margin-left, 300);
        .px2vh(margin-top, 100);
        background-color: black;
    }
    </style>
    
    定义 js 样式处理函数
    // 定义设计稿的宽高
    const designWidth = 1920;
    const designHeight = 1080;
     
    // px转vw
    export const px2vw = (_px) => {
      return (_px * 100.0) / designWidth + 'vw';
    };
     
    export const px2vh = (_px) => {
      return (_px * 100.0) / designHeight + 'vh';
    };
     
    export const px2font = (_px) => {
      return (_px * 100.0) / designWidth + 'vw';
    };
    
    

    相关文章

      网友评论

          本文标题:前端大屏之vw vh

          本文链接:https://www.haomeiwen.com/subject/gfvmpdtx.html