0

I am try to change the background color of slider.But my code is not working properly. Can anyone help me please?

here is my code

html

<a href="#" @click.prevent="redColor">Change Color</a>
<input type="range" :class="{background:fillColor}" class="slider-range" id="range" value="0" name="range" min="0" step="1" max="8">

js code

     var vm = new Vue({

          el:'#app',
          data:{
            fillColor:''
          },
          methods:{
            redColor:function(){
                this.fillColor = 'linear-gradient(90deg, #f5f4f4 0, rgb(209, 39, 16) 100%)';


            },

          }

     });
4
  • 3
    You are setting the class, not the style on the element… although I do recommend setting/changing the class and then use CSS to apply the linear-gradient style to that class. Commented Apr 13, 2020 at 19:55
  • 2
    Usestyle instead of class Commented Apr 13, 2020 at 19:56
  • @AlexBrohshtut got it thanks... Commented Apr 13, 2020 at 19:57
  • Ask yourself — "Why am I setting the background to red?" — is it because this is now the active element? because it is invalid? Those are states, and classes are excellent for representing states on elements. Now you might have <input type="range" class="active"…> where you'll be adding & removing the "active" class as needed. Then create a style in CSS that says input.active { …linear-gradient(… } — No more fiddling with styles in your code, which shouldn't care about style, it should care about state. Commented Apr 13, 2020 at 20:27

2 Answers 2

1

Switch

:class="{background:fillColor}" 

to

:style="{background:fillColor}" 
Sign up to request clarification or add additional context in comments.

Comments

0

Try this! You could use :style but I try to stay away from inline CSS even with scoped styling.

HTML

<a href="#" v-on:click="bgColor='red-color'">Change Color</a>
<input type="range" :class="bgColor" class="slider-range" id="range" value="0" name="range" min="0" step="1" max="8">

<style scoped>
.red-color {
    background: linear-gradient(90deg, #f5f4f4 0, rgb(209, 39, 16) 100%);
}
</style>

JS

var vm = new Vue({
    el:'#app',
    data:{
        bgColor:''
    },
    methods:{
    }
});

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.