美文网首页
前端大屏之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 实现大屏自适应 视口单位中的“视口”,PC端指的是浏览器的可视区域。 vw:1vw 等于视口宽度...

  • css3 vw/vh rem em

    1. vw/vh vw:1vw等于视口宽度的1%vh:1vh等于视口高度的1%vh和vw是相对于适口的高度和宽度,...

  • CSS新增单位

    CSS3 新增的单位:**vh、vw、vmin、vmax** * vw 和 vh 1、1vw 等于1/100的...

  • css3 vh and vw

    1.vw:1vw等于视口宽度的1%。 2.vh:1vh等于视口高度的1%。 3.vmin:选取vw和vh中最...

  • css3 尺寸

    1,vw、vh、vmin、vmax 的含义 (1)vw、vh、vmin、vmax 是一种视窗单位,也是相对单位。它...

  • 【转】css3中的width:100vh以及calc(100vh

    vh/vw vh: 相对于视窗的高度, 视窗被均分为100单位的vh; vw: 相对于视窗的宽度, 视窗被均分为1...

  • 用CSS3 vh 简单实现DIV全屏居中

    vh、vw、vmin、vmax介绍 vw:视窗宽度的百分比(1vw 代表视窗的宽度为 1%)vh:视窗高度的百分比...

  • vw vh vs percent

    vw vh 和 %的区别 vw&vh都是相对于viewport的,percent相对于任何设置了width或者he...

  • vue项目中使用vw/vh

    vw/vh这个单位可以根据电脑浏览器自适应vw —— 视口宽度的 1/100;vh —— 视口高度的 1/100 ...

  • 【CSS】绝对单位和相对单位

    vh http://www.css88.com/book/css/values/length/vh.htm vw ...

网友评论

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

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