跳到主要内容

useCssVar

分类
导出大小
796 B
上次更改
8 个月前

操作 CSS 变量

演示

示例文本
示例文本,--color

用法

ts
import { 
useCssVar
} from '@vueuse/core'
import {
useTemplateRef
} from 'vue'
const
el
=
useTemplateRef
('el')
const
color1
=
useCssVar
('--color',
el
)
const
elv
=
useTemplateRef
('elv')
const
key
=
ref
('--color')
const
colorVal
=
useCssVar
(
key
,
elv
)
const
someEl
=
useTemplateRef
('someEl')
const
color2
=
useCssVar
('--color',
someEl
, {
initialValue
: '#eee' })

类型声明

ts
export interface UseCssVarOptions extends ConfigurableWindow {
  
initialValue
?: string
/** * Use MutationObserver to monitor variable changes * @default false */
observe
?: boolean
} /** * Manipulate CSS variables. * * @see https://vueuse.org.cn/useCssVar * @param prop * @param target * @param options */ export declare function
useCssVar
(
prop
:
MaybeRefOrGetter
<string | null | undefined>,
target
?:
MaybeElementRef
,
options
?: UseCssVarOptions,
):
ShallowRef
<string | undefined, string | undefined>

来源

源代码演示文档

贡献者

Anthony Fu
IlyaL
Anthony Fu
Robin
丶远方
Antério Vieira
SerKo
巴布-ch
Fernando Fernández
Waleed Khaled
木荣
JD Solanki
btea
Alex Kozack

更新日志

v12.8.0
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
d680f - 修复:在初始化时更新 variable (#4641)
v12.6.0
d5dd8 - 修复:新行为 (#4500)
v12.3.0
59f75 - feat(toValue): 废弃 @vueuse/shared 中的 toValue,转而使用 Vue 的原生函数
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v11.0.2
d0523 - 修复:将 getComputedStyle 替换为直接访问样式属性 (#4166)
fe19c - 特性(useCssVars):在 null/undefined 时移除属性 (#3821)
v10.2.0
9d34a - 修复:基于元素类列表观察更改 (#3131)

根据 MIT 许可证发布。