跳到内容

useCssVar

类别
导出大小
802 B
上次更改
5 天前

操作 CSS 变量

演示

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

用法

js
import { useTemplateRef } from 'vue'
import { useCssVar } from '@vueuse/core'

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' })

类型声明

typescript
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
babu-ch
Fernando Fernández
Waleed Khaled
木荣
JD Solanki
btea
Alex Kozack

更新日志

v12.8.0 于 2025/3/5
7432f - feat(types): 弃用 MaybeRefMaybeRefOrGetter,改用 Vue 原生类型 (#4636)
d680f - fix: 在初始化时更新 variable (#4641)
v12.6.0 于 2025/2/14
d5dd8 - fix: 新行为 (#4500)
v12.3.0 于 2025/1/2
59f75 - feat(toValue): 弃用 @vueuse/shared 中的 toValue,改用 Vue 原生类型
v12.0.0-beta.1 于 2024/11/21
0a9ed - feat!: 移除 Vue 2 支持,优化包体积和清理代码 (#4349)
v11.0.2 于 2024/8/24
d0523 - fix: 使用直接样式属性访问替换 getComputedStyle (#4166)
v11.0.0-beta.2 于 2024/7/17
fe19c - feat(useCssVars): 移除 null/undefined 上的属性 (#3821)
v10.2.0 于 2023/6/16
9d34a - fix: 基于元素类列表观察更改 (#3131)
v10.0.2 于 2023/4/14
7d001 - fix: 模块引用, 关闭 #2972
v10.0.0-beta.4 于 2023/4/13
4d757 - feat(types)!: 将 MaybeComputedRef 重命名为 MaybeRefOrGetter
0a72b - feat(toValue): 将 resolveUnref 重命名为 toValue
v10.0.0-beta.0 于 2023/3/14
ae6e1 - feat: 引入 observe 选项 (#2800)

在 MIT 许可证下发布。