Skip to content

useCssVar

类别
导出体积
802 B
上次更改
last month

操作 CSS 变量

示例

示例文字,
示例文字,--color:

Usage

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
  /**
   * 使用 MutationObserver 监听变量更改
   * @default false
   */
  observe?: boolean
}
/**
 * 操作 CSS 变量。
 *
 * @see https://vueuse.org/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

更新日志

7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
d680f - fix: update variable when initial (#4641)
d5dd8 - fix: new Behavior (#4500)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
d0523 - fix: Replace getComputedStyle with direct style property access (#4166)
fe19c - feat(useCssVars): remove property on null/undefined (#3821)
9d34a - fix: observe changes based on element class list (#3131)
7d001 - fix: module reference, close #2972
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
ae6e1 - feat: introduce observe option (#2800)