var pos = document.getElementById("#myRange").value;
var slider = document.getElementById("#myRange")
var g1;
slider.oninput = function() {
if (pos < 50) {
g1 = ((pos / 50) * 33) + 33;
document.documentElement.style.setProperty('--Rval', g1 + '%');
console.log(pos);
console.log(g1);
}
}
:root {
--RVal: 33%;
--UVal: 66%;
}
#rural_container {
grid-row: 2/4;
grid-column: 1/2;
justify-self: center;
align-self: center;
width: 25vw;
height: 25vw;
border-radius: 50%;
background-image: linear-gradient(180deg, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0) var(--Rval), #77B7D3 0);
border: 5px solid #77B7D3;
display: flex;
flex-flow: row nowrap;
justify-content: center;
align-items: center;
margin-bottom: 3vw;
}
#rural {
width: 80%;
height: auto;
}
/* everything below here is not relevant to the problem */
.migration {
background-color: #FFF8EA;
width: 100%;
height: 100vh;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(4, 1fr);
}
.slidecont {
width: 60%;
grid-row: 4;
grid-column: 1/4;
justify-self: center;
height: auto;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
z-index: 2;
overflow-x: visible;
}
.slideoverlay {
width: 60%;
grid-row: 4;
grid-column: 1/4;
justify-self: center;
height: auto;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: .125vw;
background: rgb(20, 20, 20);
overflow-x: visible;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
height: 2vw;
width: 2vw;
border-radius: 10%;
transform: rotate(45deg);
background: rgb(20, 20, 20);
cursor: pointer;
}
#dot1 {
margin-left: 1vw;
width: 10px;
height: 10px;
border-radius: 50%;
background: rgb(20, 20, 20);
}
#dot2 {
width: 10px;
height: 10px;
border-radius: 50%;
background: rgb(20, 20, 20);
}
#dot3 {
margin-right: 1vw;
width: 10px;
height: 10px;
border-radius: 50%;
background: rgb(20, 20, 20);
}
#suburban_container {
grid-row: 2/4;
grid-column: 2/3;
justify-self: center;
align-self: center;
width: 25vw;
height: 25vw;
border-radius: 50%;
background-image: linear-gradient(180deg, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0) 66%, #B4D676 0);
border: 5px solid #B4D676;
display: flex;
flex-flow: row nowrap;
justify-content: center;
align-items: center;
margin-bottom: 3vw;
}
#suburban {
width: 70%;
height: auto;
}
<section class="migration">
<div class="slidecont">
<input type="range" min="0" max="100" value="0" step=".1" class="slider" id="myRange">
</div>
<div class="slideoverlay">
<div id="dot1"></div>
<div id="dot2"></div>
<div id="dot3"></div>
</div>
<div id="rural_container">
<img id="rural" src="images/rural.png" alt="">
</div>
<div id="suburban_container">
<img id="suburban" src="images/suburban.png" alt="">
</div>
</section>
我看过其他类似的问题,但是还没有找到适合我的情况的问题。我有一个范围滑块,用于控制基本div内线性渐变的位置。这是我的code thus far
CSS有点混乱,因为这是一个更大的开发中项目的一部分,我试图包括所有相关方面。所需的输出是,随着我拖动滑块,渐变将移动。忽略额外的数学运算。
最初,我使用:
:root {
--RVal: 33%;
--UVal: 66%;
}
建立CSS变量,这些变量在javascript函数中已更改。我在这里有点不合时宜,如果这是一个简单的错误,请原谅我。
答案 0 :(得分:1)
您没有正确选择ID,而是尝试使用其CSS选择器。删除#
var pos
应该位于函数内部,以供函数使用
var slider = document.getElementById("myRange")
var g1;
slider.oninput = function() {
var pos = document.getElementById("myRange").value;
if (pos < 50) {
g1 = ((pos / 50) * 33) + 33;
document.documentElement.style.setProperty('--Rval', g1 + '%');
}
}
:root {
--RVal: 33%;
--UVal: 66%;
}
#rural_container {
grid-row: 2/4;
grid-column: 1/2;
justify-self: center;
align-self: center;
width: 25vw;
height: 25vw;
border-radius: 50%;
background-image: linear-gradient(180deg, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0) var(--Rval), #77B7D3 0);
border: 5px solid #77B7D3;
display: flex;
flex-flow: row nowrap;
justify-content: center;
align-items: center;
margin-bottom: 3vw;
}
#rural {
width: 80%;
height: auto;
}
/* everything below here is not relevant to the problem */
.migration {
background-color: #FFF8EA;
width: 100%;
height: 100vh;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(4, 1fr);
}
.slidecont {
width: 60%;
grid-row: 4;
grid-column: 1/4;
justify-self: center;
height: auto;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
z-index: 2;
overflow-x: visible;
}
.slideoverlay {
width: 60%;
grid-row: 4;
grid-column: 1/4;
justify-self: center;
height: auto;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: .125vw;
background: rgb(20, 20, 20);
overflow-x: visible;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
height: 2vw;
width: 2vw;
border-radius: 10%;
transform: rotate(45deg);
background: rgb(20, 20, 20);
cursor: pointer;
}
#dot1 {
margin-left: 1vw;
width: 10px;
height: 10px;
border-radius: 50%;
background: rgb(20, 20, 20);
}
#dot2 {
width: 10px;
height: 10px;
border-radius: 50%;
background: rgb(20, 20, 20);
}
#dot3 {
margin-right: 1vw;
width: 10px;
height: 10px;
border-radius: 50%;
background: rgb(20, 20, 20);
}
#suburban_container {
grid-row: 2/4;
grid-column: 2/3;
justify-self: center;
align-self: center;
width: 25vw;
height: 25vw;
border-radius: 50%;
background-image: linear-gradient(180deg, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0) 66%, #B4D676 0);
border: 5px solid #B4D676;
display: flex;
flex-flow: row nowrap;
justify-content: center;
align-items: center;
margin-bottom: 3vw;
}
#suburban {
width: 70%;
height: auto;
}
<section class="migration">
<div class="slidecont">
<input type="range" min="0" max="100" value="0" step=".1" class="slider" id="myRange">
</div>
<div class="slideoverlay">
<div id="dot1"></div>
<div id="dot2"></div>
<div id="dot3"></div>
</div>
<div id="rural_container">
<img id="rural" src="images/rural.png" alt="">
</div>
<div id="suburban_container">
<img id="suburban" src="images/suburban.png" alt="">
</div>
</section>