如何将标签和滑块放在同一条线上

时间:2017-09-06 16:28:18

标签: javascript css

这是我的代码,它在一行上输出滑块,然后在它下面的行上输出标签。当id在同一行上彼此相邻时。

我已尝试在标签和滑块中使用显示值,但似乎没有任何区别。

任何帮助将不胜感激。 Thx提前。

var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value;

slider.oninput = function() {
  output.innerHTML = this.value;
}
#slidecontainer {
  width: 70%;
}

.slider {
  -webkit-appearance: none;
  width: 100%;
  height: 25px;
  background: #d3d3d3;
  outline: none;
  opacity: 0.7;
  -webkit-transition: .2s;
  transition: opacity .2s;
}

.slider:hover {
  opacity: 1;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 25px;
  height: 25px;
  background: #4CAF50;
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 25px;
  height: 25px;
  background: #4CAF50;
  cursor: pointer;
}

input {
  display: inline-block;
}

label {
  display: inline-block;
  float: left;
}
<h1>Custom Range Slider</h1>
<p>Drag the slider to display the current value.</p>

<div id="slidecontainer">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
  <label>Value: <span id="demo" display=inline float=left></span></label>
</div>

2 个答案:

答案 0 :(得分:4)

您的.slider CSS类中设置了width:100%,没有留下标签的空间。减少该值可以解决问题。

此外,您的inputlabel CSS规则在这里没有任何帮助,您应该删除它们。

最后,保存滑块值的span无效:

<span id="demo" display=inline float=left>

displayfloat是CSS属性,而不是HTML属性,并且不以这种方式设置。只需删除它们。

&#13;
&#13;
<!DOCTYPE html>
<html>
<style>
#slidecontainer {
    width: 70%;
}

.slider {
    -webkit-appearance: none;
    width: 80%;  /* <-- Problem was here when width is 100% */
    height: 25px;
    background: #d3d3d3;
    outline: none;
    opacity: 0.7;
    -webkit-transition: .2s;
    transition: opacity .2s;
}

.slider:hover {
    opacity: 1;
}

/* This rule causes the label to vertically align properly */
label {
  vertical-align:middle;
  display:inline-block;
  margin-bottom:1em;
}

.slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 25px;
    height: 25px;
    background: #4CAF50;
    cursor: pointer;
}

.slider::-moz-range-thumb {
    width: 25px;
    height: 25px;
    background: #4CAF50;
    cursor: pointer;
}



</style>
<body>

  <h1>Custom Range Slider</h1>
  <p>Drag the slider to display the current value.</p>

  <div id="slidecontainer">
     <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
     <label>Value: <span id="demo"></span></label>
  </div>

  <script>
    var slider = document.getElementById("myRange");
    var output = document.getElementById("demo");
    output.innerHTML = slider.value;

    slider.oninput = function() {
      output.innerHTML = this.value;
    }
  </script>

</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

通过Flexbox非常简单,我只添加了两行:

#slidecontainer {
    width: 70%;
    display: flex;
    flex-direction: row;
}

这是片段:

&#13;
&#13;
var slider = document.getElementById("myRange");
    var output = document.getElementById("demo");
    output.innerHTML = slider.value;

    slider.oninput = function() {
      output.innerHTML = this.value;
    }
&#13;
#slidecontainer {
    width: 70%;
    display: flex;
    flex-direction: row;
}

.slider {
    -webkit-appearance: none;
    width: 100%;
    height: 25px;
    background: #d3d3d3;
    outline: none;
    opacity: 0.7;
    -webkit-transition: .2s;
    transition: opacity .2s;
}

.slider:hover {
    opacity: 1;
}

.slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 25px;
    height: 25px;
    background: #4CAF50;
    cursor: pointer;
}

.slider::-moz-range-thumb {
    width: 25px;
    height: 25px;
    background: #4CAF50;
    cursor: pointer;
}

input {
    display:inline;
}

label {
    display:inline;
    float: left;
    width: 30%;
}
&#13;
<h1>Custom Range Slider</h1>
<p>Drag the slider to display the current value.</p>

<div id="slidecontainer">
     <input type="range" min="1" max="100" value="50" class="slider" id="myRange" >
     <label>Value: <span id="demo" display=inline float=left></span></label>
</div>
&#13;
&#13;
&#13;