HTML Javascript change image with Slider bar step

后端 未结 1 1440
臣服心动
臣服心动 2021-01-27 04:18

    

        
相关标签:
1条回答
  • 2021-01-27 04:58

    I added if(newVal >= 0 && newVal < 35) with 2 conditions to check if value is in a range and then set <img>

    You can't use document.getElementById("img").src = newVal+ ".jpg";, in that case you will get 100 different images

    <input id="valR" type="range" min="0" max="100" value="0" step="5" oninput="showVal(this.value)" onchange="showVal(this.value)" />
        <span id="range">0</span>
        <img id="img">
    
    <script>
    
        var val = document.getElementById("valR").value;
            document.getElementById("range").innerHTML=val;
            document.getElementById("img").src = val + ".jpg";
            function showVal(newVal){
              document.getElementById("range").innerHTML=newVal;
              if(newVal >= 0 && newVal < 35)
                document.getElementById("img").src = "first_image.jpg";
              else if(newVal >= 35 && newVal < 65)
                document.getElementById("img").src = "second_image.png";
              else if(newVal >= 35 && newVal < 65)
                document.getElementById("img").src = "third_image.png";
            }
    </script>

    0 讨论(0)
提交回复
热议问题