【问题标题】:How to change the images based on the range slider?如何根据范围滑块更改图像?
【发布时间】:2019-12-27 10:46:56
【问题描述】:

我正在复制此网页https://www.modsy.com/project/furniture,但图像不会根据范围滑块而改变 我的html代码是:

<div class="image mt-3 mb-3">
    <img src="../static/images/7.jpg" width="400" height="180">
    <img src="../static/images/8.jpg" width="400" height="180">
    <img src="../static/images/9.jpg" width="400" height="180">
</div>
<br>
<div class="rangeslider">
    <input type="range" min="1" max="3" value="3" class="myslider" id="sliderRange">
    <div class="row mt-3">
        <div class="col-4">
            <h6 class="display-6">Starting From Scratch</h6>
            <p id="demo"> I'm designing the room </p>
        </div>
        <div class="col-4">
            <h6 class="display-6">Somewhere in Between</h6>
            <p class="demo">I'm designing around a few pieces I already own</p>
        </div>
        <div class="col-4">
            <h6 class="display-6">Mostly Furnished</h6>
            <p class="demo">I want to put the finishing touches on my room</p>
        </div>
    </div>
</div>

我的 JS 代码:

<script> 
   var rangeslider = document.getElementById("sliderRange"); 
   var output = document.getElementById("demo"); 
   output.innerHTML = rangeslider.value; 
   rangeslider.oninput = function() { 
       output.innerHTML = this.value; 
  } 
</script> 

我的 CSS 代码:

<style> 

.rangeslider{ 
    width: 50%; 
} 

.myslider { 
    -webkit-appearance: none; 
    background: #FCF3CF  ; 
    width: 50%; 
    height: 20px; 
    opacity: 2; 
   } 


.myslider::-webkit-slider-thumb { 
    -webkit-appearance: none; 
    cursor: pointer; 
    background: #34495E  ; 
    width: 5%; 
    height: 20px; 
} 


.myslider:hover { 
    opacity: 1; 
} 

</style> 

错误是:未捕获的类型错误:无法读取 null 的属性“值” 你能说我在代码中犯了什么错误

【问题讨论】:

  • 尝试将脚本标签移动到 html 文档的底部
  • @Ryan 我收到错误,因为 Uncaught TypeError: Cannot set property 'innerHTML' of null 你能帮帮我吗
  • 滑动范围滑块时图像也没有变化如何实现这一点

标签: javascript html


【解决方案1】:

您应该使用load event 或将您的脚本标签移到正文标签的末尾。

我正在更改样式属性,但最好的方法是使用类来很好地扩展代码。

window.addEventListener('load', function() {

  var rangeslider = document.getElementById("sliderRange");

  var images = document.getElementById("sliderImages");

  rangeslider.addEventListener('input', function() {
    for (var i = 0; i < images.children.length; i++) {
      images.children[i].style.display = 'none';
    }
    i = Number(this.value) - 1;
    images.children[i].style.display = 'block';
  });

});
.rangeslider {
  width: 400px;
  margin: 0 auto;
}

.myslider {
  -webkit-appearance: none;
  background: #FCF3CF;
  width: 100%;
  height: 20px;
  opacity: 0.8;
  margin-top: 100px;
}

.myslider::-webkit-slider-thumb {
  -webkit-appearance: none;
  cursor: pointer;
  background: #34495E;
  width: 33%;
  height: 20px;
}

.myslider:hover {
  opacity: 1;
}

.image {
  position: relative;
  width: 400px;
  margin: 0 auto;
}

.image>img {
  position: absolute;
  display: none;
}

.image>img.visible,
.image>img:first-child {
  display: block;
}

.sliderOutput>div {
  margin: 5px;
  width: 120px;
  display: inline-block;
  vertical-align: top;
  text-align: center;
}

.sliderOutput h6,
.sliderOutput p {
  margin: 5px;
}
<div class="image mt-3 mb-3" id="sliderImages">
  <img src="https://via.placeholder.com/400x100.png?text=Image1" width="400" height="100">
  <img src="https://via.placeholder.com/400x100.png?text=Image2" width="400" height="100">
  <img src="https://via.placeholder.com/400x100.png?text=Image3" width="400" height="100">
</div><br>
<div class="rangeslider">
  <input type="range" min="1" max="3" value="1" class="myslider" id="sliderRange">
  <div class="sliderOutput row mt-3">
    <div class="col-4">
      <h6 class="display-6">Starting From Scratch</h6>
      <p class="demo"> I'm designing the room </p>
    </div>
    <div class="col-4">
      <h6 class="display-6">Somewhere in Between</h6>
      <p class="demo">I'm designing around a few pieces I already own</p>
    </div>
    <div class="col-4">
      <h6 class="display-6">Mostly Furnished</h6>
      <p class="demo">I want to put the finishing touches on my room</p>
    </div>
  </div>
  </p>
</div>

【讨论】:

  • @ raf 代码有效,但我应该在行类型中有三个文本,如此处所示,只有图像应该更改 modsy.com/project/furniture 但随着您的代码文本正在更改,它不会以行格式显示
  • @raf 你能说这个吗
  • 我必须将三个文本并排排列,但我应该更改图像
【解决方案2】:

您的 JS 代码运行得太早了。 DOM 尚未准备好。

将脚本标签移到页面末尾会有所帮助(正如上面提到的@RyanSparks)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2020-06-14
    相关资源
    最近更新 更多