what is vue vw

青灯夜游
Release: 2022-12-20 18:19:12
Original
3199 people have browsed it

在vue中,vw是一种视口单位,是根据窗口的宽度,分成100等份,100vw就表示满宽,50vw就表示一半宽;vw始终是针对窗口的宽。vh和vw单位主要用于页面视口大小布局,可以根据电脑浏览器自适应,在页面布局上更加方便简单。

what is vue vw

本教程操作环境:windows7系统、vue3版,DELL G3电脑。

传统的项目开发中,我们只会用到px%em这几个单位,它可以适用于大部分的项目开发,且拥有比较良好的兼容性

从CSS3开始,浏览器对计量单位的支持又提升到了另外一个境界,新增了remvhvwvm等一些新的计量单位

利用这些新的单位开发出比较良好的响应式页面,适应多种不同分辨率的终端,包括移动设备等。

Vue项目中也使用vw实现移动端适配。

vh、vw

vw ,就是根据窗口的宽度,分成100等份,100vw就表示满宽,50vw就表示一半宽。(vw 始终是针对窗口的宽),同理,vh则为窗口的高度

这里的窗口分成几种情况:

  • 在桌面端,指的是浏览器的可视区域

  • 移动端指的就是布局视口

像vw、vh,比较容易混淆的一个单位是%,不过百分比宽泛的讲是相对于父元素:

  • 对于普通定位元素就是我们理解的父元素

  • 对于position: absolute;的元素是相对于已定位的父元素

  • 对于position: fixed;的元素是相对于 ViewPort(可视窗口)

vh、vw:主要用于页面视口大小布局,在页面布局上更加方便简单

vue项目中使用vw/vh

vw/vh这个单位可以根据电脑浏览器自适应

  • vw —— 视口宽度的 1/100;

  • vh —— 视口高度的 1/100 

在pc端,视口宽高就是浏览器得宽高;

在项目中引入插件

npm install postcss-import postcss-loader postcss-px-to-viewport --save-dev
npm i postcss-px-to-viewport-opt
Copy after login

然后在根目录下面创建postcss.config.js

module.exports = {
    plugins: {
        "autoprefixer": {
            path: ['./src/*']
        },
        "postcss-import": {},
        "postcss-px-to-viewport-opt": {
            "viewportWidth": "1920", //视窗的宽度,对应的是我们设计稿的宽度
            "viewportHeight": "1080", // 视窗的高度
            "unitPrecision": 2, //指定`px`转换为视窗单位值的小数位数(很多时候无法整除)
            "viewportUnit": "vw", //指定需要转换成的视窗单位,建议使用vw
            "selectorBlackList": ['#nprogress'], //指定不转换为视窗单位的类
            "minPixelValue": 1, // 小于或等于`1px`不转换为视窗单位
            "mediaQuery": false, // 允许在媒体查询中转换`px`
            // "exclude": /(\/|\\)(node_modules)(\/|\\)/
        },
    }
};
Copy after login

然后重启项目

之后就可以在页面中写px自动转换成vw了

The above is the detailed content of what is vue vw. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!