最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I change CSS variable on button click? - Stack Overflow

programmeradmin0浏览0评论

How can I make CSS change the button's box-shadow from var('--out') to var('--in') on button click, and when you click it again it'll change back to --out?

I tried doing it but whenever I click the button it just removes the whole shadow.

const btn = document.querySelector('button')
const state = '--out'
btn.addEventListener('click', _ =>{
    document.documentElement.style.setProperty(state, '--in')
})
:root{
--in: inset 25px 25px 60px rgba(0,0,0,.5);
--out: inset -25px -25px 60px rgba(0,0,0,.5);
}
.color_1{
background-color: blue;
border-radius: 200px;
height: 300px;
width: 300px;
box-shadow: var(--out);
}
<body>
<section>
<button class="color_1"/>
</section>
</body>

How can I make CSS change the button's box-shadow from var('--out') to var('--in') on button click, and when you click it again it'll change back to --out?

I tried doing it but whenever I click the button it just removes the whole shadow.

const btn = document.querySelector('button')
const state = '--out'
btn.addEventListener('click', _ =>{
    document.documentElement.style.setProperty(state, '--in')
})
:root{
--in: inset 25px 25px 60px rgba(0,0,0,.5);
--out: inset -25px -25px 60px rgba(0,0,0,.5);
}
.color_1{
background-color: blue;
border-radius: 200px;
height: 300px;
width: 300px;
box-shadow: var(--out);
}
<body>
<section>
<button class="color_1"/>
</section>
</body>

Share Improve this question edited Dec 23, 2022 at 10:59 Bilal 3,9346 gold badges32 silver badges67 bronze badges asked Feb 1, 2022 at 14:23 MoeMoe 1151 gold badge2 silver badges6 bronze badges 2
  • You would need to modify the stylesheet, or change the clasname of the element so, that the new class contains --in variable. – Teemu Commented Feb 1, 2022 at 14:39
  • You can modify the variable too, that was your original goal. – Teemu Commented Feb 1, 2022 at 16:34
Add a ment  | 

4 Answers 4

Reset to default 2

You are trying to change the value of one variable with another variable.

You will need getComputedStlye():

:root{
    --in: inset 25px 25px 60px red;
    --out: inset -25px -25px 60px green;
}
.color_1{
    background-color: blue;
    border-radius: 200px;
    height: 300px;
    width: 300px;
    box-shadow: var(--out);
}
<body>
<section>
<button class="color_1">

</button>

<script>
    const btn = document.querySelector('button')
    const state = '--out'
    btn.addEventListener('click', _ =>{  document.documentElement.style.setProperty(state, getComputedStyle(document.documentElement).getPropertyValue('--in'));
    });
</script>

</section>
</body>

But the ideal way would not be to change the value of your root variable. What if you need that value somewhere else. You can have separate classes for this on your element, which you add/toggle on a conditional basis.

:root{
    --in: inset 25px 25px 60px red;
    --out: inset -25px -25px 60px green;
}
.color_1{
    background-color: blue;
    border-radius: 200px;
    height: 300px;
    width: 300px;
}

.shadow1{
    box-shadow: var(--out);
}
.shadow2{
    box-shadow: var(--in);
}
<body>
<section>
<button class="color_1 shadow1">

</button>

<script>
    const btn = document.querySelector('button')
    const state = '--out'
    btn.addEventListener('click', function() {
    //this.classList.remove('shadow1');
    this.classList.toggle('shadow2');
    });
</script>

</section>
</body>

You need to use the :root selector with document.querySelector instead of document.documentElement to change the css variable. You also need to change the css to use a new --state variable that you can toggle between --in and --out with the JS code. You will also need to use getProperty to get the value of the --in or --out css variable before changing it. The following code in the script tag should fix your problem:

const btn = document.querySelector('button')
const state = '--in'
btn.addEventListener('click', _ =>{
    var root = document.querySelector(":root")
    root.style.setProperty("--state", root.style.getProperty(state))
})

And also using the following in your style tag:

:root{
    --in: inset 25px 25px 60px rgba(0,0,0,.5);
    --out: inset -25px -25px 60px rgba(0,0,0,.5);
    --state: var(--out);
}
.color_1{
    background-color: blue;
    border-radius: 200px;
    height: 300px;
    width: 300px;
    box-shadow: var(--state);
}

Ok here's what I got for you!

  1. It was removing --out value and setting it to --in looked like this (--out: --in;). Which is why we were seeing the box-shadow being removed

  2. This code: document.documentElement - was not targeting your button background - change it to: btn.style.boxShadow

const btn = document.querySelector('button');
   const state = '--out'
   btn.addEventListener('click', _ =>{
       document.documentElement.style.boxShadow = 'var(--in)';
   })

You can define two possible states and toogle between them.

<body>
    <style>
        :root{
            --in: inset 25px 25px 60px rgba(0,0,0,.5);
            --out: inset -25px -25px 60px rgba(0,0,0,.5);
            --one: blue;
            --two: red;
        }
        .color_1{
            background-color: var(--one);
            border-radius: 200px;
            height: 300px;
            width: 300px;
            box-shadow: var(--in);
        }

        .color_2{
            background-color: var(--two);
            border-radius: 200px;
            height: 300px;
            width: 300px;
            box-shadow: var(--out);
        }
    </style>
    
<section>
<button class="color_1">

</button>

<script>
    const btn = document.querySelector('button')
    const state = '--out'
    btn.addEventListener('click', _ =>{
        states = ['color_1', 'color_2']
        actualClassName = btn.classList[0]
        btn.classList.remove(actualClassName)
        if (actualClassName === states[0]){
            btn.classList.add(states[1])
        } 
        else{
            btn.classList.add(states[0])
        }
    })
</script>

</section>
</body>

Finally, the best solution would be toogle only the state you desire, like this:

<body>
    <style>
        :root{
            --in: inset 25px 25px 60px rgba(0,0,0,.5);
            --out: inset -25px -25px 60px rgba(0,0,0,.5);
            --one: blue;
            --two: red;
        }

        .color_1{
            background-color: blue;
            border-radius: 200px;
            height: 300px;
            width: 300px;
        }

        .state1{
            box-shadow: var(--out);
        }
        
        .state2{
            box-shadow: var(--in);
        }
    </style>
    
<section>
<button class="color_1 state1">

</button>

<script>
    const btn = document.querySelector('button')
    const state = '--out'
    btn.addEventListener('click', _ =>{
        btn.classList.toggle('state2');
    });
</script>

</section>
</body>

发布评论

评论列表(0)

  1. 暂无评论