px转为rem

rem是一个单位,相对于html的fontsize而言。经常用来适配移动端页面

逻辑像素:软件所能达到的分辨率,我们开发实际使用的分辨率,eg:320X568

物理像素(设备像素):设备实际的像素,就是我们平时买手机说的分辨率,是设计人员使用的分辨率,eg:640X1136

逻辑像素和物理像素之间存在一个缩放因子,有兴趣的可以去看看retina屏。

虽然移动端和pc端都是单位都是px,但是这两个px却是不同的,前者是逻辑像素,后者是物理像素。

 

iPhone尺寸规格

设备

苹果手机

宽度

高度

对角线

对角线

逻辑分辨率(点)

比例因子

设备分辨率(像素)

PPI

3GS

2.4英寸(62.1毫米)

4.5英寸(115.5毫米)

3.5英寸

 

小320x480

@ 1X

小320x480

163

4(S)

2.31英寸(58.6毫米)

4.5英寸(115.2毫米)

3.5英寸

小320x480

@ 2倍

640×960

326

5C

2.33英寸(59.2毫米)

4.90英寸(124.4毫米)

4英寸

320x568

@ 2倍

640x1136

326

5(S)

2.31英寸(58.6毫米)

4.87英寸(123.8毫米)

4英寸

320x568

@ 2倍

640x1136

326

6

2.64英寸(67.1毫米)

5.44英寸(138.3毫米)

4.7英寸

375x667

@ 2倍

750x1334

326

6+

3.07英寸(77.9毫米)

6.23英寸(158.2毫米)

5.5英寸

414x736

@ 3倍

1242x2208 - >)

1080×1920

401 

 

 

我们现在一般设计图一般基于iphone的,一般是640或750,fonsize是40px。

要使用rem,可以分为两步

  1). 计算出html的fontsize:(按640设计稿)------- 这段代码应该放在body之前,因为应该在渲染页面之前就计算好,避免出现闪烁问题。

 

<script>
    function adjustFontsize() {
      var docEle = document.documentElement;
      var width = (screen.width > 0) ? (window.innerWidth >= screen.width || window.innerWidth == 0) ? screen.width :
        window.innerWidth : window.innerWidth;
      if (width) {
        width = Math.min(width, 750);
        docEle.style.fontSize = 20 * (width / 375) + 'px';
      }
    }
    
    var evt = 'onorientationchange' in window ? 'orientationchange' : 'resize';
    window.addEventListener(evt, adjustFontsize, false);

    adjustFontsize();
  </script>

  2). px转换为rem

 

      可以使用sublime的插件cssrem,调整下fontsize

      可以使用scss的函数功能实现

 

@function rem($target, $context: 40px) {
  @return ($target / $context) * 1rem;
}


使用的时候

 

 

padding: rem(10px) 0;

 

 

 

 

 

 

 

 

 

 

在Vue 3中使用TypeScript和Vite开发项目时,将像素px转换为rem有几种方法。以下是其中一种常用的方法: 1. 首先,为了方便转换,你可以在根组件中设置一个基准字体大小。在`App.vue`文件中,你可以添加以下代码: ```vue <template> <div id="app"> <!-- your app content --> </div> </template> <script> export default { created() { // 根据设计稿的尺寸设置基准字体大小,例如设计稿宽度为750px,基准字体大小为100px const baseFontSize = 100; const designWidth = 750; const screenWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; document.documentElement.style.fontSize = `${(screenWidth / designWidth) * baseFontSize}px`; }, }; </script> <style> /* 全局样式中可以使用rem单位 */ body { font-size: 16px; /* 可以使用px作为备用单位 */ } </style> ``` 通过以上代码,我们可以将根元素的字体大小设置为屏幕宽度的比例乘以基准字体大小,从而实现根据屏幕宽度自动调整字体大小。 2. 在项目中使用`postcss-pxtorem`插件来自动将px单位转换为rem单位。首先,安装插件: ```shell npm install postcss-pxtorem --save-dev ``` 然后,在项目根目录下创建一个`postcss.config.js`文件,配置插件: ```javascript module.exports = { plugins: { 'postcss-pxtorem': { rootValue: 100, // 设置基准字体大小,与设计稿一致 propList: ['*'], // 需要转换的属性,这里表示所有属性都需要转换 }, }, }; ``` 最后,在`vite.config.js`文件中添加postcss插件配置: ```javascript import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue'; export default defineConfig({ plugins: [vue()], css: { postcss: { plugins: [require('postcss-pxtorem')], }, }, }); ``` 这样,在你的Vue组件中使用px单位时,它们将自动转换为rem单位。 这是一种常用的方法。当然,还有其他一些工具和方法可以实现将px转换为rem,请根据你的项目需求选择适合的方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值