gpt4 book ai didi

javascript - 使用 js 更改输入 slider 的颜色渐变?

转载 作者:可可西里 更新时间:2023-11-01 13:20:51 26 4
gpt4 key购买 nike

我已经完成了以下问题,但没有一个能得到我想要的答案:-(所以请不要链接它们并将其标记为重复)。

  1. Using JavaScript to edit CSS gradient
  2. Is there a way to style HTML5's range control?
  3. How to customize the HTML5 input range type looks using CSS?
  4. Change color of input range slider with javascript [duplicate]

我有三个这样的 slider 。

它们是我使用了渐变的 RGB slider 。在红色 slider 中,绿色和蓝色值为 0。即红色 slider 的渐变值:rgb(0,0,0) 到 rgb(255,0,0)。

enter image description here

我想在使用 javascript 滑动红色 slider 时更改蓝色 slider 和绿色 slider 的渐变。如果我将红色值更改为 120。然后在蓝色 slider 中,渐变必须从 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将其更改为不同的颜色渐变。

最佳答案

在这种情况下,我会考虑使用 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>

关于javascript - 使用 js 更改输入 slider 的颜色渐变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48985608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com