javascript - 一起更改两个范围 - javascript

标签 javascript html range

我有三个范围..一个范围更改宽度第二个范围更改高度..我想要第三个范围更改宽度和高度以及相同的值(更改大小)。

function x11() {
  var x1 = document.getElementById("m1").value;
  var mm = document.getElementById("mm").style;
  mm.width = x1 + "px";
}

function x22() {
  var x1 = document.getElementById("m2").value;
  var mm = document.getElementById("mm").style;
  mm.height = x1 + "px";
}
1<input type="range" min="0" max="200" value="100" style="width:100%;" oninput="x11(this.value)" id="m1">
   
2<input type="range" min="0" max="200" value="40" step="0.5" style="width:100%;" oninput="x22(this.value)" id="m2">
    3
<input type="range" style="width:100%;"  id="m3">

<p id="mm" style="width: 150px; height:40px;  background-color:red;"></p>

最佳答案

如果您想按比例更改宽度和高度,则需要将它们的值保存在某处。这样你就有了工作的引用点。然后只需添加这些引用点即可。

var width = 100;
var height = 40;
var extra = 0;

// Actually use the passed in value
function x11(x1) {
  var mm = document.getElementById("mm").style;
  width = Number(x1);
  mm.width = (width + extra) + "px";
}

function x22(x1) {
  var mm = document.getElementById("mm").style;
  height = Number(x1);
  mm.height = (height + extra) + "px";
}

function x33(x1) {
  // Change the size proportionally
  var mm = document.getElementById("mm").style;
  extra = Number(x1);
  mm.width = (width + extra) + 'px';
  mm.height = (height + extra) + 'px';
}
1<input type="range" min="0" max="200" value="100" style="width:100%;" oninput="x11(this.value)" id="m1">
   
2<input type="range" min="0" max="200" value="40" step="0.5" style="width:100%;" oninput="x22(this.value)" id="m2">
    3
<input type="range" style="width:100%;" oninput="x33(this.value)" min="0" max="200" value="0" id="m3">

<p id="mm" style="width: 100px; height:40px;  background-color:red;"></p>

上面的方法可以工作,但我个人会稍微清理一下。

var width = 100;
var height = 40;
var margin = 0;

function setWidth(w) {
  width = Number(w);
  drawBox();
}

function setHeight(h) {
  height = Number(h);
  drawBox();
}

function setMargin(m) {
  margin = Number(m);
  drawBox();
}

// Move box drawing to one function
function drawBox() {
  var box = document.getElementById('box').style;
  box.width = (width + margin) + 'px';
  box.height = (height + margin) + 'px';
}

// Avoid having to set the initial size in CSS
drawBox();
/* Separate CSS from HTML */
input {
  width: 100%;
}

p {
  background-color: red;
}
<!-- We don't need the IDs since we're getting the value directly -->
<!-- We're also going to use more descriptive function names -->
1 <input type="range" min="0" max="200" value="100" oninput="setWidth(this.value)">
   
2 <input type="range" min="0" max="200" value="40" step="0.5" oninput="setHeight(this.value)">
    
3 <input type="range" min="0" max="200" value="0" oninput="setMargin(this.value)">

<p id="box"></p>

关于javascript - 一起更改两个范围 - javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39170488/

相关文章:

Javascript textarea 撤消重做

javascript - 有条件地从数据库中按多列获取行的函数

html - 响应式布局中图片比例的解决方案

html - 居中网格 - 内部网格左对齐 - 无 Javascript - 无媒体查询

c++ - 用 'grouped'元素迭代 vector ?

algorithm - 自上而下的范围合并?

javascript - nodeJS 创建动态数组并用作响应

javascript - 使用 jquery 获取类 x 的最后一个元素 'each'

html - 带边框的递归 DIV : the base case

python - 使用 Python range 内置函数从整数向后计数,与解析树相关