html640设计稿,设计稿及尺寸单位

在 Taro 中尺寸单位建议使用 px、 百分比 %,Taro 默认会对所有单位进行转换。在 Taro 中书写尺寸按照 1:1 的关系来进行书写,即从设计稿上量的长度 100px,那么尺寸书写就是 100px,当转成微信小程序的时候,尺寸将默认转换为 100rpx,当转成 H5 时将默认转换为以 rem 为单位的值。

如果你希望部分 px 单位不被转换成 rpx 或者 rem ,最简单的做法就是在 px 单位中增加一个大写字母,例如 Px 或者 PX 这样,则会被转换插件忽略。

结合过往的开发经验,Taro 默认以 750px 作为换算尺寸标准,如果设计稿不是以 750px 为标准,则需要在项目配置 config/index.js 中进行设置,例如设计稿尺寸是 640px,则需要修改项目配置 config/index.js 中的 designWidth 配置为 640:const config = {

projectName: 'myProject',

date: '2018-4-18',

designWidth: 640,

....

}

目前 Taro 支持 750、 640 、 828 三种尺寸设计稿,他们的换算规则如下:const deviceRatio = {

'640': 2.34 / 2,

'750': 1,

'828': 1.81 / 2

}

建议使用 Taro 时,设计稿以 iPhone 6 750px 作为设计尺寸标准。

如果你的设计稿是 375 ,不在以上三种之中,那么你需要把 designWidth 配置为 375,同时在 deviceRatio 中添加换算规则如下:{

designWidth: 375,

deviceRatio: {

'375': 1 / 2,

'640': 2.34 / 2,

'750': 1,

'828': 1.81 / 2

}

}

API

在编译时,Taro 会帮你对样式做尺寸转换操作,但是如果是在 JS 中书写了行内样式,那么编译时就无法做替换了,针对这种情况,Taro 提供了 API Taro.pxTransform 来做运行时的尺寸转换。Taro.pxTransform(10) // 小程序:rpx,H5:rem

配置

默认配置会对所有的 px 单位进行转换,有大写字母的 Px 或 PX 则会被忽略。

参数默认值如下:{

onePxTransform: true,

unitPrecision: 5,

propList: ['*'],

selectorBlackList: [],

replace: true,

mediaQuery: false,

minPixelValue: 0

}

Type: Object | Null

onePxTransform (Boolean)

设置 1px 是否需要被转换

unitPrecision (Number)

REM 单位允许的小数位。

propList (Array)

允许转换的属性。Values need to be exact matches.

Use wildcard * to enable all properties. Example: ['*']

Use * at the start or end of a word. (['*position*'] will match background-position-y)

Use ! to not match a property. Example: ['*', '!letter-spacing']

Combine the "not" prefix with the other prefixes. Example: ['*', '!font*']

selectorBlackList

黑名单里的选择器将会被忽略。If value is string, it checks to see if selector contains the string.['body'] will match .body-class

If value is regexp, it checks to see if the selector matches the regexp.[/^body$/] will match body but not .body

replace (Boolean)

直接替换而不是追加一条进行覆盖。

mediaQuery (Boolean)

允许媒体查询里的 px 单位转换

minPixelValue (Number)

设置一个可被转换的最小 px 值

配置规则对应到 config/index.js ,例如:{

h5: {

publicPath: '/',

staticDirectory: 'static',

postcss: {

autoprefixer: {

enable: true

},

pxtransform: {

enable: true,

config: {

selectorBlackList: ['body']

}

}

}

},

mini: {

// ...

postcss: {

pxtransform: {

enable: true,

config: {

selectorBlackList: ['body']

}

}

}

}

}

忽略

属性

当前忽略单个属性的最简单的方法,就是 px 单位使用大写字母。/* `px` is converted to `rem` */

.convert {

font-size: 16px; // converted to 1rem

}

/* `Px` or `PX` is ignored by `postcss-pxtorem` but still accepted by browsers */

.ignore {

border: 1Px solid; // ignored

border-width: 2PX; // ignored

}

文件

对于头部包含注释 /*postcss-pxtransform disable*/ 的文件,插件不予处理。

设计响应式页面时,使用rem单位是一种相对于根元素字体大小的单位,可以实现页面的自适应布局。假设设计稿的宽度为750px,那么我们可以将根元素的字体大小设置为75px,这样1rem就等于10px,方便计算。以下是详细的rem适配布局步骤: 1. 设置viewport 在页面的head标签中添加viewport meta标签,用于设置设备的视口大小,例如: ```html <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> ``` 2. 设置根元素字体大小 在CSS中,设置根元素的字体大小为设计稿宽度的1/10,即: ```css html { font-size: 75px; } ``` 3. 以rem为单位进行布局 在CSS中,我们使用rem作为长度单位,例如: ```css /* 以设计稿上的字体大小为基准 */ h1 { font-size: 48px; } /* 以设计稿上的元素尺寸为基准 */ .container { width: 600px; height: 300px; } /* 以设计稿上的间距为基准 */ .item { margin: 20px; } ``` 4. 通过媒体查询设置不同屏幕下的根元素字体大小 为了适应不同屏幕宽度的设备,可以通过媒体查询设置不同屏幕下的根元素字体大小,例如: ```css /* 在屏幕宽度小于等于320px时,根元素字体大小为32px */ @media screen and (max-width: 320px) { html { font-size: 32px; } } /* 在屏幕宽度介于321px~640px之间时,根元素字体大小为64px */ @media screen and (min-width: 321px) and (max-width: 640px) { html { font-size: 64px; } } ``` 通过以上步骤,我们可以实现一个基于rem单位的自适应布局,使页面在不同设备上都能够呈现出较好的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值