0

For example, I want to define a global color a = '#FFF', and reference it in js and css to make sure that there is only one color named a in the project. then when the value of a changed, a in js and css also changed. is that possible in vue?

1 Answer 1

1

Hmm... I was thinking about watchers watch in combination with CSS variables.

Maybe something like this? Whenever a changes, the CSS variable --a changes aswell.

You can actually type in any color format you want. Hex, rgb, rgba...

let v = new Vue({
   el: "#app",
   data: {
      a: "red"
   },
   watch: {
      a(val){
         document.documentElement.style.setProperty("--a", val);
      }
   }
})
:root {
   --a: red;
}
#app {
   height: 100px;
   width:100%;
   background: var(--a);
   transition: background 500ms;
}

p {
   background: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
   <p>Color: {{a}}</p>
   <input v-model="a">
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.