使用js更改输入滑块的颜色渐变?
Posted
技术标签:
【中文标题】使用js更改输入滑块的颜色渐变?【英文标题】:Change color gradient of input slider using js? 【发布时间】:2018-08-05 17:43:29 【问题描述】:我已经解决了以下问题,但没有一个有我想要的答案:-(所以请不要链接它们并将其标记为重复)。
-
Using javascript to edit CSS gradient
Is there a way to style html5's range control?
How to customize the HTML5 input range type looks using CSS?
Change color of input range slider with javascript [duplicate]
我有三个这样的滑块。
它们是我使用渐变的 RGB 滑块。 在红色滑块中,绿色和蓝色值为 0。即红色滑块的渐变值:rgb(0,0,0) 到 rgb(255,0,0)。
当我使用 javascript 滑动红色滑块时,我想更改蓝色滑块和绿色滑块的渐变。 如果我将红色值更改为 120。 然后在蓝色滑块中,渐变必须从 rgb(old_red_value,0,old_green_value) 更改为 rgb(changed_red_value,255,old_green_value)
用于为 div 定义颜色。
<head>
<style type="text/css">
.slider
display: inline;
-webkit-appearance: none;
height: 15px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
-webkit-transition: .2s;
transition: opacity .2s;
width: 80%;
margin-left: 5%;
#red_slider
background: linear-gradient(to right, rgb(0,0,0) , rgb(255,0,0) );
#green_slider
background: linear-gradient(to right, rgb(0,0,0) , rgb(0,255,0) );
#blue_slider
background: linear-gradient(to right, rgb(0,0,0) , rgb(0,0,255) );
</style>
</head>
<body>
<input type="range" min="0" max="255" value="127" class="slider" id="red_slider">
<p>Red: <span id="red_value"></span></p>
<input type="range" min="0" max="255" value="127" class="slider" id="green_slider">
<p>Green: <span id="green_value"></span></p>
<input type="range" min="0" max="255" value="127" class="slider" id="blue_slider">
<p>Blue: <span id="blue_value"></span></p>
<script type="text/javascript">
var red_slider = document.getElementById("red_slider");
var green_slider = document.getElementById("green_slider");
var blue_slider = document.getElementById("blue_slider");
var red_output = document.getElementById("red_value");
var green_output = document.getElementById("green_value");
var blue_output = document.getElementById("blue_value");
red_output.innerHTML = red_slider.value;
green_output.innerHTML = green_slider.value;
blue_output.innerHTML = blue_slider.value;
var red_val = red_slider.value;
var green_val = green_slider.value;
var blue_val = blue_slider.value;
red_slider.oninput = function()
red_output.innerHTML = this.value;
red_val = this.value;
disp.style.backgroundColor = "rgb("+red_val+","+green_val+","+blue_val+")";
rgb_value.innerHTML = "rgb("+red_val+","+green_val+","+blue_val+")";
// hex_value.innerHTML = "hex("+red_val.toString(16) +","+green_val.toString(16) +","+blue_val.toString(16) +")";
// blue_slider.style.background = "black";
// blue_slider.css
// background: linear-gradient(to right, rgb($red_val,0,0) , rgb($red_val,0,255) );
//
// blue_slider.style.background = "linear-gradient(to right, rgb("+red_val+",0,0) , rgb("+red_val+",0,255) );";
green_slider.oninput = function()
green_output.innerHTML = this.value;
green_val = this.value;
disp.style.backgroundColor = "rgb("+red_val+","+green_val+","+blue_val+")";
rgb_value.innerHTML = "rgb("+red_val+","+green_val+","+blue_val+")";
// hex_value.innerHTML = "rgb("+red_val+","+green_val+","+blue_val+")";
blue_slider.oninput = function()
blue_output.innerHTML = this.value;
blue_val = this.value;
disp.style.backgroundColor = "rgb("+red_val+","+green_val+","+blue_val+")";
rgb_value.innerHTML = "rgb("+red_val+","+green_val+","+blue_val+")";
// hex_value.innerHTML = "rgb("+red_val+","+green_val+","+blue_val+")";
</script>
我可以使用 js 将其更改为固定颜色 -
blue_slider.style.background = "black";
,不过我的问题是用js把它改成不同的颜色渐变。
【问题讨论】:
分享你的代码 @TemaniAfif 抱歉忘记添加代码...用代码更新了我的问题 【参考方案1】:在这种情况下,我会考虑使用 CSS 变量来简化代码。思路是用JS改变颜色变量,代码会减少。
然后你可以通过在需要的地方使用变量并且只使用 CSS 来调整渐变颜色:
var red_slider = document.getElementById("red_slider");
var green_slider = document.getElementById("green_slider");
var blue_slider = document.getElementById("blue_slider");
var red_output = document.getElementById("red_value");
var green_output = document.getElementById("green_value");
var blue_output = document.getElementById("blue_value");
red_slider.oninput = function()
red_output.innerHTML = this.value;
document.documentElement.style.setProperty('--red-color', this.value);
green_slider.oninput = function()
green_output.innerHTML = this.value;
document.documentElement.style.setProperty('--green-color', this.value);
blue_slider.oninput = function()
blue_output.innerHTML = this.value;
document.documentElement.style.setProperty('--blue-color', this.value);
:root
--red-color:127;
--blue-color:127;
--green-color:127;
.slider
display: inline;
-webkit-appearance: none;
height: 15px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
-webkit-transition: .2s;
transition: opacity .2s;
width: 80%;
margin-left: 5%;
.slider::-webkit-slider-thumb
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
.slider::-moz-range-thumb
width: 25px;
height: 25px;
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
#rgb_value_container
/*margin: 16px auto;*/
text-align: center;
font-weight: bold;
font-size: 20px;
#red_slider
background: linear-gradient(to right, rgb(0, 0, 0), rgb(255, var(--green-color), var(--blue-color)));
#green_slider
background: linear-gradient(to right, rgb(0, 0, 0), rgb(var(--red-color), 255, var(--blue-color)));
#blue_slider
background: linear-gradient(to right, rgb(0, 0, 0), rgb(var(--red-color), var(--green-color), 255));
#color_box
width: 250px;
height: 250px;
margin: 30px auto;
background-color: rgb(var(--red-color), var(--green-color), var(--blue-color));
<div id="color_box"></div>
<input type="range" min="0" max="255" value="127" class="slider" id="red_slider">
<p>Red: <span id="red_value">127</span></p>
<input type="range" min="0" max="255" value="127" class="slider" id="green_slider">
<p>Green: <span id="green_value">127</span></p>
<input type="range" min="0" max="255" value="127" class="slider" id="blue_slider">
<p>Blue: <span id="blue_value">127</span></p>
【讨论】:
以上是关于使用js更改输入滑块的颜色渐变?的主要内容,如果未能解决你的问题,请参考以下文章