Home > Blockchain >  Can i manipulate a specific part of a css property value?
Can i manipulate a specific part of a css property value?

Time:05-18

What i want?

I want to manipulate a specific part of a css property value with an HTML range input.

/* an example CSS */
.background-img {
background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),
    url(wallpaper.jpg);
}

Let's say i want to change only the alpha (opacity) parts of this css rgba code with my html range input here;

<input
 
 type="range"
 min="0"
 max="0.999"
 value="0.5"
 step="0.010"
                />

Is it a possible thing to manipulate a specific part of a css property value with js? How can i connect this html input element into background-image css property value, not to whole value but the opacity part or parts.

CodePudding user response:

The following might serve as a starting point for what you are trying to do:

var input = document.getElementById("range-input");
let root = document.documentElement;

input.onchange = function()
{
  root.style.setProperty('--opacity', input.value);
};
:root {
  --opacity: 1.0;
}

.background-img {
width: 200px;
height: 100px;
background-size: 200px 100px;
background-image: url(https://variety.com/wp-content/uploads/2021/07/Rick-Astley-Never-Gonna-Give-You-Up.png?w=1024);
opacity: var(--opacity);
}
<input
 
 id="range-input"
 type="range"
 min="0"
 max="0.999"
 value="0.5"
 step="0.010"
                />

<div ></div>

  • Related