0

I want to create a background color of body element that will dynamically change based on this.linearGradient. As you might see it should change a part of linear-gradient instead of replacing the entire linear-gradient. No inline-styling allows.

computed: {
  backgroundStyle () {
    return {
      '--linearGradient': this.linearGradient
    }
  }
},

<style>
background: linear-gradient(145.74deg, #9BDBFF -33.02%, #B4DEDA 52.01%, #FFD66B  var(--linearGradient)7.04%);
</style>
  <body :style="backgroundStyle">
    <div id="app"></div>
  </body>

1 Answer 1

1

Please find the below snippet.

new Vue({
  el: "#app",
  template: '<div>bg-color<br/>Applied<input type="text" v-model="linearGradient" /></div>',
  data() {
    return {
      linearGradient: '#FFD66B'
    }
  },
  watch: {
    linearGradient: {
      immediate: true,
      handler(oldValue, newValue) {
        document.body.style.background = `linear-gradient(145.74deg, #9BDBFF -33.02%, #B4DEDA 52.01%,  ${oldValue} 7.04%)`
      }
    }
  },
  computed: {
    backgroundStyle: function() {
      return {
        background: `linear-gradient(145.74deg, #9BDBFF -33.02%, #B4DEDA 52.01%,  ${this.linearGradient} 7.04%)`
      }
    }
  }
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<div id="app"></div>

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

2 Comments

How do I pass this to parent element. body element is in index.html instead of component....
document.body.style.background contains a typo....but nevertheless this is exactly what I was looking for...

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.