简体   繁体   English

如何使所有滑块都在一条垂直线上?

[英]how to make all of the sliders in one vertical line?


I Have a form that includes a label, slider and an output. 我有一个包含 label、slider 和 output 的表格。
Different sizes of labels can mess the vertical organization of the slider (like in the link below). 不同大小的标签会弄乱 slider 的垂直组织(如下面的链接)。
dist 距离

This is my code:这是我的代码:

 function slider(name,item){ var label=document.createElement("label"); label.innerHTML = name+" rate for "+item; var form=document.createElement("form"); form.name="registrationForm"; form.className ="mainForm"; var input = document.createElement("input"); input.type="range"; input.name="ageInputName"; input.id="input"+name+item; input.value="50"; input.min="0"; var output = document.createElement("output"); output.name="ageOutputName"; output.id="output"+name+item; input.oninput=()=>updateTextInput(input.id,output.id); form.appendChild(label); form.appendChild(input); form.appendChild(output); output.value="50"; document.getElementById("container").appendChild(form); }

How can I fix this?我怎样才能解决这个问题?

You can use css classes with attributes width and display:inline-block to achieve what you are looking for.您可以使用 css 具有属性 width 和 display:inline-block 的类来实现您正在寻找的内容。 Example In your javascript you can use input.classList.add('input');示例在您的 javascript 中,您可以使用input.classList.add('input');

HTML HTML

.label {
  width:150px;
  background:#f7f7f7;
  display:inline-block;
  text-align:center;
}
.input {
  width:200px;
  display:inline-block;
}
.output {
  width:50px;
  background:#d7d7d7;
  display:inline-block;
}

-- if it has to be done in js you can always add styles in js to your elements -- 如果它必须在 js 中完成,你总是可以在 js 中添加 styles到你的元素

JS JS

slider('first', 'bag');
slider('second', 'box');
slider('third', 'floor');

function slider(name, item) {
  var label = document.createElement("label");
  label.innerHTML = name + " rate for " + item;
  label.classList.add("label");

  var form = document.createElement("form");
  form.name = "registrationForm";
  form.className = "mainForm";

  var input = document.createElement("input");
  input.type = "range";
  input.name = "ageInputName";
  input.id = "input" + name + item;
  input.value = "50";
  input.min = "0";
  input.classList.add('input');

  var output = document.createElement("output");
  output.name = "ageOutputName";
  output.id = "output" + name + item;
  output.classList.add('output');

  input.oninput = () => updateTextInput(input.id, output.id);

  form.appendChild(label);
  form.appendChild(input);
  form.appendChild(output);
  output.value = "50";
  document.getElementById("container").appendChild(form);

}

For a slightly more advanced method you could learn about css grid and flexbox .对于稍微高级的方法,您可以了解css 网格flexbox

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

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