diff --git a/src/area/README.md b/src/area/README.md index 9c10fdb57..4a60b074b 100644 --- a/src/area/README.md +++ b/src/area/README.md @@ -64,7 +64,7 @@ To have a selected value,simply pass the `code` of target area to `value` prop | area-list | Area list data | _object_ | - | | columns-placeholder `v2.2.5` | Placeholder of columns | _string[]_ | `[]` | | loading | Whether to show loading prompt | _boolean_ | `false` | -| item-height `v2.8.6` | Option height, supports `px` ans `rem` unit, default `px` | _number \| string_ | `44` | +| item-height `v2.8.6` | Option height, supports `px` `vw` `rem` unit, default `px` | _number \| string_ | `44` | | columns-num | Level of picker | _number \| string_ | `3` | | visible-item-count | Count of visible columns | _number \| string_ | `5` | | swipe-duration `v2.2.13` | Duration of the momentum animation,unit `ms` | _number \| string_ | `1000` | diff --git a/src/area/README.zh-CN.md b/src/area/README.zh-CN.md index 71f8367e5..0de44a86a 100644 --- a/src/area/README.zh-CN.md +++ b/src/area/README.zh-CN.md @@ -64,7 +64,7 @@ Vue.use(Area); | area-list | 省市区数据,格式见下方 | _object_ | - | | columns-placeholder `v2.2.5` | 列占位提示文字 | _string[]_ | `[]` | | loading | 是否显示加载状态 | _boolean_ | `false` | -| item-height `v2.8.6` | 选项高度,支持 `px` 和 `rem` 单位,默认 `px` | _number \| string_ | `44` | +| item-height `v2.8.6` | 选项高度,支持 `px` `vw` `rem` 单位,默认 `px` | _number \| string_ | `44` | | columns-num | 显示列数,3-省市区,2-省市,1-省 | _number \| string_ | `3` | | visible-item-count | 可见的选项个数 | _number \| string_ | `5` | | swipe-duration `v2.2.13` | 快速滑动时惯性滚动的时长,单位`ms` | _number \| string_ | `1000` | diff --git a/src/datetime-picker/README.md b/src/datetime-picker/README.md index bd27f1e51..3293d5e20 100644 --- a/src/datetime-picker/README.md +++ b/src/datetime-picker/README.md @@ -259,7 +259,7 @@ export default { | filter | Option filter | _(type, vals) => vals_ | - | | formatter | Option text formatter | _(type, val) => val_ | - | | columns-order `v2.9.2` | Array for ordering columns, where item can be set to
`year`, `month`, `day`, `hour` and `minute` | _string[]_ | - | -| item-height `v2.8.6` | Option height, supports `px` ans `rem` unit, default `px` | _number \| string_ | `44` | +| item-height `v2.8.6` | Option height, supports `px` `vw` `rem` unit, default `px` | _number \| string_ | `44` | | visible-item-count | Count of visible columns | _number \| string_ | `5` | | swipe-duration `v2.2.13` | Duration of the momentum animation,unit `ms` | _number \| string_ | `1000` | diff --git a/src/datetime-picker/README.zh-CN.md b/src/datetime-picker/README.zh-CN.md index 22f7d54f8..f7a39741a 100644 --- a/src/datetime-picker/README.zh-CN.md +++ b/src/datetime-picker/README.zh-CN.md @@ -268,7 +268,7 @@ export default { | filter | 选项过滤函数 | _(type, vals) => vals_ | - | | formatter | 选项格式化函数 | _(type, val) => val_ | - | | columns-order `v2.9.2` | 自定义列排序数组, 子项可选值为
`year`、`month`、`day`、`hour`、`minute` | _string[]_ | - | -| item-height `v2.8.6` | 选项高度,支持 `px` 和 `rem` 单位,默认 `px` | _number \| string_ | `44` | +| item-height `v2.8.6` | 选项高度,支持 `px` `vw` `rem` 单位,默认 `px` | _number \| string_ | `44` | | visible-item-count | 可见的选项个数 | _number \| string_ | `5` | | swipe-duration `v2.2.13` | 快速滑动时惯性滚动的时长,单位`ms` | _number \| string_ | `1000` | diff --git a/src/picker/README.md b/src/picker/README.md index a2654b023..d6da938eb 100644 --- a/src/picker/README.md +++ b/src/picker/README.md @@ -252,7 +252,7 @@ export default { | show-toolbar | Whether to show toolbar | _boolean_ | `false` | | allow-html `v2.1.8` | Whether to allow HTML in option text | _boolean_ | `true` | | default-index | Default value index of single column picker | _number \| string_ | `0` | -| item-height `v2.8.6` | Option height, supports `px` ans `rem` unit, default `px` | _number \| string_ | `44` | +| item-height `v2.8.6` | Option height, supports `px` `vw` `rem` unit, default `px` | _number \| string_ | `44` | | visible-item-count | Count of visible columns | _number \| string_ | `5` | | swipe-duration `v2.2.10` | Duration of the momentum animation,unit `ms` | _number \| string_ | `1000` | diff --git a/src/picker/README.zh-CN.md b/src/picker/README.zh-CN.md index 9d0fdad33..8ba6d01a5 100644 --- a/src/picker/README.zh-CN.md +++ b/src/picker/README.zh-CN.md @@ -275,7 +275,7 @@ export default { | show-toolbar | 是否显示顶部栏 | _boolean_ | `false` | | allow-html `v2.1.8` | 是否允许选项内容中渲染 HTML | _boolean_ | `true` | | default-index | 单列选择时,默认选中项的索引 | _number \| string_ | `0` | -| item-height `v2.8.6` | 选项高度,支持 `px` 和 `rem` 单位,默认 `px` | _number \| string_ | `44` | +| item-height `v2.8.6` | 选项高度,支持 `px` `vw` `rem` 单位,默认 `px` | _number \| string_ | `44` | | visible-item-count | 可见的选项个数 | _number \| string_ | `5` | | swipe-duration `v2.2.10` | 快速滑动时惯性滚动的时长,单位 `ms` | _number \| string_ | `1000` | diff --git a/src/sticky/README.md b/src/sticky/README.md index fcb804a05..17ce81ccd 100644 --- a/src/sticky/README.md +++ b/src/sticky/README.md @@ -56,7 +56,7 @@ export default { | Attribute | Description | Type | Default | | --- | --- | --- | --- | -| offset-top `v2.8.7` | Offset top, supports `px` ans `rem` unit, default `px` | _number \| string_ | `0` | +| offset-top `v2.8.7` | Offset top, supports `px` `vw` `rem` unit, default `px` | _number \| string_ | `0` | | z-index | z-index when sticky | _number \| string_ | `99` | | container | Container DOM | _Element_ | - | diff --git a/src/sticky/README.zh-CN.md b/src/sticky/README.zh-CN.md index e26650d6f..696d07055 100644 --- a/src/sticky/README.zh-CN.md +++ b/src/sticky/README.zh-CN.md @@ -66,7 +66,7 @@ export default { | 参数 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | -| offset-top `v2.8.7` | 吸顶时与顶部的距离,支持 `px` 和 `rem` 单位,默认 `px` | _number \| string_ | `0` | +| offset-top `v2.8.7` | 吸顶时与顶部的距离,支持 `px` `vw` `rem` 单位,默认 `px` | _number \| string_ | `0` | | z-index | 吸顶时的 z-index | _number \| string_ | `99` | | container | 容器对应的 HTML 节点 | _Element_ | - | diff --git a/src/sticky/test/__snapshots__/index.spec.js.snap b/src/sticky/test/__snapshots__/index.spec.js.snap index be6374a1d..fbfac5d00 100644 --- a/src/sticky/test/__snapshots__/index.spec.js.snap +++ b/src/sticky/test/__snapshots__/index.spec.js.snap @@ -36,6 +36,14 @@ exports[`offset-top with rem unit 1`] = ` `; +exports[`offset-top with vw unit 1`] = ` +
+
+ Content +
+
+`; + exports[`sticky to top 1`] = `
diff --git a/src/sticky/test/index.spec.js b/src/sticky/test/index.spec.js index 7fb2de770..16c6b2c2f 100644 --- a/src/sticky/test/index.spec.js +++ b/src/sticky/test/index.spec.js @@ -63,6 +63,22 @@ test('offset-top with rem unit', () => { window.getComputedStyle = originGetComputedStyle; }); +test('offset-top with vw unit', () => { + window.innerWidth = 300; + + const wrapper = mount({ + template: ` + + Content + + `, + }); + + mockScrollTop(100); + expect(wrapper).toMatchSnapshot(); + mockScrollTop(0); +}); + test('should not trigger scroll event when hidden', () => { const scroll = jest.fn(); mount({ diff --git a/src/tab/README.md b/src/tab/README.md index 009dc2637..567c562ab 100644 --- a/src/tab/README.md +++ b/src/tab/README.md @@ -205,7 +205,7 @@ In scrollspy mode, the list of content will be tiled | swipeable | Whether to switch tabs with swipe gestrue in the content | _boolean_ | `false` | | lazy-render | Whether to enable tab content lazy render | _boolean_ | `true` | | scrollspy `v2.3.0` | Whether to use scrollspy mode | _boolean_ | `false` | -| offset-top `v2.8.7` | Sticky offset top , supports `px` ans `rem` unit, default `px` | _number \| string_ | `0` | +| offset-top `v2.8.7` | Sticky offset top , supports `px` `vw` `rem` unit, default `px` | _number \| string_ | `0` | | swipe-threshold | Set swipe tabs threshold | _number \| string_ | `4` | - | | title-active-color | Title active color | _string_ | - | | title-inactive-color | Title inactive color | _string_ | - | diff --git a/src/tab/README.zh-CN.md b/src/tab/README.zh-CN.md index 757ea4aeb..1c62490e3 100644 --- a/src/tab/README.zh-CN.md +++ b/src/tab/README.zh-CN.md @@ -209,7 +209,7 @@ export default { | swipeable | 是否开启手势滑动切换 | _boolean_ | `false` | | lazy-render | 是否开启延迟渲染(首次切换到标签时才触发内容渲染) | _boolean_ | `true` | | scrollspy `v2.3.0` | 是否开启滚动导航 | _boolean_ | `false` | -| offset-top `v2.8.7` | 粘性定位布局下与顶部的最小距离,支持 `px` 和 `rem` 单位,默认 `px` | _number \| string_ | `0` | +| offset-top `v2.8.7` | 粘性定位布局下与顶部的最小距离,支持 `px` `vw` `rem` 单位,默认 `px` | _number \| string_ | `0` | | swipe-threshold | 滚动阈值,标签数量超过阈值时开始横向滚动 | _number \| string_ | `4` | | title-active-color | 标题选中态颜色 | _string_ | - | | title-inactive-color | 标题默认态颜色 | _string_ | - | diff --git a/src/utils/format/unit.ts b/src/utils/format/unit.ts index b50167e7c..9b2de011d 100644 --- a/src/utils/format/unit.ts +++ b/src/utils/format/unit.ts @@ -1,4 +1,4 @@ -import { isDef } from '..'; +import { isDef, inBrowser } from '..'; import { isNumeric } from '../validate/number'; export function addUnit(value?: string | number): string | undefined { @@ -30,13 +30,24 @@ function convertRem(value: string) { return +value * getRootFontSize(); } +function convertVw(value: string) { + value = value.replace(/vw/g, ''); + return (+value * window.innerWidth) / 100; +} + export function unitToPx(value: string | number): number { if (typeof value === 'number') { return value; } - if (value.indexOf('rem') !== -1) { - return convertRem(value); + if (inBrowser) { + if (value.indexOf('rem') !== -1) { + return convertRem(value); + } + + if (value.indexOf('vw') !== -1) { + return convertVw(value); + } } return parseFloat(value);