首页 > 解决方案 > 使用范围滑块调整线性渐变

问题描述

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 中线性渐变的位置。到目前为止,这是我的代码

CSS 有点混乱,因为这是一个更大的开发项目的一部分,我试图包括所有相关方面。所需的输出是当我拖动滑块时渐变会移动。忽略额外的数学。

最初,我使用:

:root {
    --RVal: 33%;
    --UVal: 66%;
}

建立CSS变量,这些变量在javascript函数中改变。我在这里有点不合群,所以如果这是一个简单的错误,请原谅我。

标签: javascripthtmlcsslinear-gradientscss-variables

解决方案


  • 您没有正确选择 id,您正在尝试使用它的 css 选择器。消除#

  • varpos应该位于函数内部以供函数使用

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>


推荐阅读