简体   繁体   English

使用范围滑块更改div的高度

[英]Changing height of div with range slider

I am trying to change the height of a div with a range slider. 我正在尝试使用范围滑块更改div的高度。

 var heightR = document.getElementById("heightR").value; var details = document.getElementById("details"); function range() { heightD.value=value; } function changeH() { if (heightR = 1) { details.style.height = "100px"; } else if (heightR = 2) { details.style.height = "200px"; } else if (heightR = 3) { details.style.height = "300px"; } else { console.log("Less than 1, higher than 3 or NaN"); } } 
 <div id="generator"> <input id="heightR" type="range" min="1" max="3" value="1" onchange="heightD.value=value;changeH();"></input> <output id="heightD">1</output> <div id="details" style="border: solid 1px black"><p>Content</p></div> <br /> </div> 

As you see, whenever the range is moved the height of the div is set to 100px, as if the value of the range slider was always 1... I checked in the console, and it is indeed! 如您所见,只要移动范围,div的高度就会设置为100px,好像范围滑块的值始终为1 ...我在控制台中进行了检查,的确如此! I tried changing the value attribute of heightR to 2, and it did the same thing: the value always equaled 2?! 我尝试将heightR的value属性更改为2,它做了同样的事情:该值始终等于2 ?!

Any help? 有什么帮助吗?

You need to put "var heightR = document.getElementById("heightR").value;" 您需要输入“ var heightR = document.getElementById(“ heightR”)。value;“ inside the changeH function and also change if else function from "=" to "==" for comparing the value of heightR with "1/2/3". 在changeH函数中,并且还将else函数从“ =”更改为“ ==”,以将heightR的值与“ 1/2/3”进行比较。 Hope this help ^^ 希望这个帮助^^

 var heightR = document.getElementById("heightR").value; var details = document.getElementById("details"); function range() { heightD.value=value; } function changeH() { var heightR = document.getElementById("heightR").value; if (heightR == 1) { details.style.height = "100px"; } else if (heightR == 2) { details.style.height = "200px"; } else if (heightR == 3) { details.style.height = "300px"; } else { console.log("Less than 1, higher than 3 or NaN"); } } 
 <div id="generator"> <input id="heightR" type="range" min="1" max="3" value="1" onchange="heightD.value=value;changeH();"></input> <output id="heightD">1</output> <div id="details" style="border: solid 1px black"><p>Content</p></div> <br /> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM