Vue injects CSS variables

Related tags

Vue.js vue-cssvar
Overview

vue-cssvar

Vue 注入 CSS 变量控制样式 (兼容 vue2 , vue3)

样例:点击 div。div 以及其 hover伪类会变色。

<template>
	<div v-css="{a,b}" class="box" @click="changeColor"></div>
</template>
export default {
  data(){
    return {
      a : 'blue',
      b : 'pink'
    }
  },
  methods: {
    changeColor(){
      this.a = this.a === 'blue' ? "red" : "blue"
      this.b = this.b === 'pink' ? "yellow" : "pink"
    }
  }
}
</script>
<style>
  .box {
    width : 100px;
    height : 100px;
    background-color : var(--a);
  }
  .box:hover {
    background-color : var(--b);
  }
</style>

简便了 vue 处理元素 style ,以及可以直接修改伪类伪元素的样式。

使用

vue3 注册指令

import { createApp } from 'vue'
import vueCssvar from 'vue-cssvar'
const app = createApp(App)
vueCssvar(app)
app.mount('#app')

vue2注册指令

import Vue from 'vue'
import vueCssvar from 'vue-cssvar'
vueCssvar(Vue)
new Vue({
  render: function (h) { return h(App) },
}).$mount('#app')

vueCssvar 参数

  1. 参数1 : 指令要绑定的应用,及 vue2 的 Vue、vue3 的 app。
  2. 参数2 : 相关配置 options
  • options.name 指令名,默认 'css'
vueCssvar(app,{
	name : 'custom'
})
	
-----------template-------------
<div v-custom="{a,b}" class="box" @click="changeColor"></div>
	
  • options.isPx 值为数字时默认加上px方便计算,默认 true
vueCssvar(app,{
	isPx : 'true'
})
	
-----------template-------------
<div v-css="{c,b}" class="box" @click="changeColor">hello</div>
<script>
export default {
data(){
  return {
    c : 1,
    b : '700' //如果不必须后面加px,使用字符串类型
  }
},
methods: {
  changeColor(){
    this.c++;
  }
}
}
</script>
<style>
.box {
// 使用 css 内置函数计算其 width,此时的var(--c)为 1px
  width : calc( var(--c) * 100); 
  height : 100px;
  background-color : red;
  font-weight: var(--b);
}
</style>

	
  • options.toLine 驼峰命名法转中划线命名法(中划线命名法符合css规范),默认 false
vueCssvar(app,{
	toLine : true
})
	
-----------template-------------
<div v-css="{userAvatarColor}" class="box" @click="changeColor"></div>

-----------script---------------
data(){
  return { 
    userAvatarColor: 'green'
  }
}

-----------style-----------------
color : var(--user-avatar-color)
	
You might also like...

The first truly composable CSS animation library. Built for Vue, React, SCSS, and CSS, AnimXYZ will bring your website to life.

AnimXYZ animxyz.com AnimXYZ helps you create, customize, and compose animations for your website. Powered by CSS variables to allow a nearly limitless

Jan 2, 2023

Build performant, native and cross-platform desktop applications with native Vue + powerful CSS like styling.🚀

Build performant, native and cross-platform desktop applications with native Vue + powerful CSS like styling.🚀

Vue NodeGui Build performant, native and cross-platform desktop applications with Vue. 🚀 Vue NodeGUI is powered by Vue 🌈 and Qt5 💚 which makes it C

Dec 30, 2022

Mobile UI Components, based on Vue.js and ionic CSS

Mobile UI Components, based on Vue.js and ionic CSS

Mobile UI Components, based on Vue.js and ionic CSS

Dec 23, 2022

📓 The UI component explorer. Develop, document, & test React, Vue, Angular, Web Components, Ember, Svelte & more!

📓 The UI component explorer. Develop, document, & test React, Vue, Angular, Web Components, Ember, Svelte & more!

Build bulletproof UI components faster Storybook is a development environment for UI components. It allows you to browse a component library, view the

Jan 9, 2023

A Vue.js 2.0 UI Toolkit for Web

A Vue.js 2.0 UI Toolkit for Web

A Vue.js 2.0 UI Toolkit for Web. Element will stay with Vue 2.x For Vue 3.0, we recommend using Element Plus from the same team Links Homepage and doc

Jan 3, 2023

The Intuitive Vue Framework

The Intuitive Vue Framework

Build your next Vue.js application with confidence using Nuxt: a framework making web development simple and powerful. Links 📘 Documentation: https:/

Jan 5, 2023

🐉 Material Component Framework for Vue

🐉 Material Component Framework for Vue

Supporting Vuetify Vuetify is a MIT licensed project that is developed and maintained full-time by John Leider and Heather Leider; with support from t

Jan 3, 2023

🛠️ Standard Tooling for Vue.js Development

Vue CLI Vue CLI is the Standard Tooling for Vue.js Development. Documentation Docs are available at https://cli.vuejs.org/ - we are still working on r

Jan 4, 2023

🗃️ Centralized State Management for Vue.js.

Vuex 🔥 HEADS UP! You're currently looking at Vuex 3 branch. If you're looking for Vuex 4, please check out 4.0 branch. Vuex is a state management pat

Dec 30, 2022
Owner
siluxianren
Idlers watch the road
siluxianren
A Nuxt.js module that injects a server route to serve the GraphQL Playground

@pin-pon/nuxt-graphql-playground A Nuxt.js module that injects a server route to serve the GraphQL Playground Setup Add @pin-pon/nuxt-graphql-playgrou

Pin-Pon 3 Sep 22, 2022
✅ Vite plugin for validating your environment variables

This Vite plugin allows you to validate your environment variables at build or dev time. This allows your build/dev-server to fail-fast if your setup

Julien Ripouteau 61 Dec 23, 2022
:tada: A magical vue admin https://panjiachen.github.io/vue-element-admin

English | 简体中文 | 日本語 | Spanish SPONSORED BY 活动服务销售平台 客户消息直达工作群 Introduction vue-element-admin is a production-ready front-end solution for admin inter

花裤衩 80.1k Dec 31, 2022
:eyes: Vue in React, React in Vue. Seamless integration of the two. :dancers:

vuera NOTE: This project is looking for a maintainer! Use Vue components in your React app: import React from 'react' import MyVueComponent from './My

Aleksandr Komarov 4k Dec 30, 2022
🎉 基于 vite 2.0 + vue 3.0 + vue-router 4.0 + vuex 4.0 + element-plus 的后台管理系统vue3-element-admin

vue3-element-admin ?? 基于 Vite 2.0 + Vue3.0 + Vue-Router 4.0 + Vuex 4.0 + element-plus 的后台管理系统 简介 vue3-element-admin 是一个后台前端解决方案,它基于 vue3 和 element-plu

雪月欧巴 84 Nov 28, 2022
Jenesius vue modal is simple library for Vue 3 only

Jenesius Vue Modal Jenesius vue modal is simple library for Vue 3 only . Site Documentation Installation npm i jenesius-vue-modal For add modals in yo

Архипцев Евгений 63 Dec 30, 2022
A template repository / quick start to build Azure Static Web Apps with a Node.js function. It uses Vue.js v3, Vue Router, Vuex, and Vite.js.

Azure Static Web App Template with Node.js API This is a template repository for creating Azure Static Web Apps that comes pre-configured with: Vue.js

Marc Duiker 6 Jun 25, 2022
Mosha-vue-toastify - A light weight and fun Vue 3 toast or notification or snack bar or however you wanna call it library.

Mosha Vue Toastify A lightweight and fun Vue 3 toast or notification or snack bar or however you wanna call it library. English | 简体中文 Talk is cheap,

Baidi Liu 187 Jan 2, 2023
A plugin that can help you create project friendly with Vue for @vue/cli 4.5

vue-cli-plugin-patch A plugin that can help you create project friendly with Vue for @vue/cli 4.5. Install First you need to install @vue/cli globally

null 2 Jan 6, 2022
Veloce: Starter template that uses Vue 3, Vite, TypeScript, SSR, Pinia, Vue Router, Express and Docker

Veloce Lightning-fast cold server start Instant hot module replacement (HMR) and dev SSR True on-demand compilation Tech Stack Vue 3: UI Rendering lib

Alan Morel 10 Oct 7, 2022