admin管理员组文章数量:1430035
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
4 Answers
Reset to default 2You 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!
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 removedThis 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>
本文标签: javascriptHow can I change CSS variable on button clickStack Overflow
版权声明:本文标题:javascript - How can I change CSS variable on button click? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745498400a2660914.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论