首页 > 解决方案 > 框内可调整大小的范围输入滑块

问题描述

我正在尝试使用 jQuery 可调整大小/可拖动的工作范围输入滑块。我知道这里有一些熟悉的问题和解决方案,但它们不符合我的需求。我遇到的问题是,在调整字段大小时,我希望滑块沿东西方向均匀调整大小。由于某种原因,这些数字没有显示出来。对于如何解决这个问题,有任何的建议吗?事先我不得不说这是代码世界中每个人的最佳资源,我非常感谢任何帮助。谢谢你。

$(function() {
  $(".field-wrapper").draggable({
      handle: ".handle"
    })
    .resizable({
      handles: "all",
      resize: function(e, ui) {
        var s = ui.size;
        $(".field", this).width(s.width - 40).height(s.height);
        $(".handle", this).height(Math.round(s.height / 2) + 20).css("margin-top", (Math.round(s.height / 2) - 20) + "px");
      }
    });
});
.field-wrapper {
  width: 245px;
  border: 2px inset #ccc;
  border-radius: 26px;
  padding: 0;
}

.field-wrapper .handle {
  display: inline-block;
  cursor: move;
}

.field-wrapper .field {
  border: 0;
  padding: 0;
  padding-left: .5em;
  margin: -1px 0;
  border-left: 1px solid #ccc;
  height: 5em
}
 
input[type='range'] {
  box-sizing: border-box;
  border: 0px solid transparent;
  padding: 0px;
  margin: 0px;
  width: 210px;
  height: 50px;
  cursor: pointer;
  background: -webkit-repeating-linear-gradient(90deg, #777, #777 1px, transparent 1px, transparent 40px) no-repeat 50% 50%;
  background: -moz-repeating-linear-gradient(90deg, #777, #777 1px, transparent 1px, transparent 40px) no-repeat 50% 50%;
  background: repeating-linear-gradient(90deg, #777, #777 1px, transparent 1px, transparent 40px) no-repeat 50% 50%;
  background-size: 122px 25px;
  font-size: 16px;
}
input[type='range'],
input[type='range']::-webkit-slider-runnable-track,
input[type='range']::-webkit-slider-thumb {
  -webkit-appearance: none;
}
input[type='range']::-webkit-slider-runnable-track {
  box-sizing: border-box;
  width: 200px;
  height: 5px;
  border-radius: 2px;
  background: #777;
}
input[type='range']::-moz-range-track {
  box-sizing: border-box;
  width: 200px;
  height: 5px;
  border-radius: 2px;
  padding: 0px;
  background: #777;
}
input[type='range']::-moz-range-thumb {
  box-sizing: border-box;
  padding: 0px;
  height: 20px;
  width: 10px;
  border-radius: 2px;
  border: 1px solid;
  background: #EEE;
}
input[type='range']::-ms-track {
  box-sizing: border-box;
  width: 210px;
  height: 5px;
  border-radius: 2px;
  padding: 0px;
  background: #777;
  color: #777;
}
input[type='range']::-webkit-slider-thumb {
  box-sizing: border-box;
  padding: 0px;
  height: 20px;
  width: 10px;
  border-radius: 2px;
  border: 1px solid;
  margin-top: -8px;
  background: #EEE;
}
input[type='range']::-ms-thumb {
  box-sizing: border-box;
  padding: 0px;
  height: 20px;
  width: 10px;
  border-radius: 2px;
  border: 1px solid;
  background: #EEE;
}
input[type="range"]::-ms-fill-lower {
  background: transparent;
}
input[type='range']:focus {
  outline: none;
}
.field-wrapper rangeinput:after {
  position: absolute;
  color: #777;
  content: '20 40 60 80';
  padding: 40px;
  word-spacing: 20px;
  left: 0px;
  top: 0px;
  z-index: -1;
}
.field-wrapper rangeinput {
  padding: 0px;
  position: relative;
}

.field-wrapper rangeinput output{
  display: block;
  margin-top: 20px;
  color: #777;
}
.field-wrapper rangeinput output:before{
  content:"Selected Value: ";
  font-weight: bold;
}
.field-wrapper rangeinput body {
  font-family: Calibri, Arial;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="field-wrapper">
  <span class="handle ui-icon ui-icon-grip-dotted-vertical"></span>
  <input type="range" min="0" max="100" value="50" step="1" list="tickmarks" id="rangeInput" oninput="output.value = rangeInput.value">
  <datalist id="tickmarks">
    <option value="0 to 20">0</option>
    <option>20</option>
    <option>40</option>
    <option>60</option>
    <option>80</option>
    <option>100</option>
  </datalist>
</div>

标签: javascriptjqueryhtmlcss

解决方案


为了在调整大小时始终对齐中间,您需要设置position:absolute然后设置顶部和左右偏移试试这个:(我还修复了显示当前滑块值和水平问题)

$(function() {
  $(".field-wrapper").draggable({
      handle: ".handle"
      
    })
    .resizable({
      handles: "all",
      resize: function(e, ui) {
        var s = ui.size;
        $(".field", this).width(s.width - 40).height(s.height);
        $(".handle", this).height(Math.round(s.height / 2)).css("top", (Math.round(s.height / 2)-5) + "px");
        
      }
    });
});
 $("#rangeInput").on("change", function(){
 
 $("#amount").html(this.value);
 });
.field-wrapper {
max-width: 1300px;
border: 2px inset
#ccc;
border-radius: 26px;
padding: 0;
display: block;
position: relative;
left: 33px;
top: 44px;
height: 112px;
width: 254px;
min-height: 50px;
}

.field-wrapper .handle {
  display: inline-block;
  cursor: move;
  top:40%;
}

.field-wrapper .field {
  border: 0;
  padding: 0;
  padding-left: .5em;
  margin: -1px 0;
  border-left: 1px solid #ccc;
  height: 5em
}
 #amount
 {
 font-weight:bold;
   text-align: center;
display: block;
position: absolute;
top: 10%;
left: 0;
right: 0;
 }
input[type='range'] {
  box-sizing: border-box;
border: 0px solid
transparent;
padding: 0px;
margin: 0px;
width: 90%;
height: auto;
cursor: pointer;
background: -webkit-repeating-linear-gradient(90deg, #777, #777 1px, transparent 1px, transparent 40px) no-repeat 50% 50%;
background: -moz-repeating-linear-gradient(90deg, #777, #777 1px, transparent 1px, transparent 40px) no-repeat 50% 50%;
background: repeating-linear-gradient(90deg,
#777, #777 1px, transparent 1px,
transparent 40px) no-repeat 50% 50%;
    background-size: auto;
background-size: 122px 25px;
font-size: 16px;
position: absolute;
top: 40%;
bottom: auto;
right: 0;
margin: 0px auto;
left: 0;
}
input[type='range'],
input[type='range']::-webkit-slider-runnable-track,
input[type='range']::-webkit-slider-thumb {
  -webkit-appearance: none;
}
input[type='range']::-webkit-slider-runnable-track {
  box-sizing: border-box;
  width: 200px;
  height: 5px;
  border-radius: 2px;
  background: #777;
}
input[type='range']::-moz-range-track {
  box-sizing: border-box;
 
  height: 5px;
  border-radius: 2px;
  padding: 0px;
  background: #777;
}
input[type='range']::-moz-range-thumb {
  box-sizing: border-box;
  padding: 0px;
  height: 20px;
  width: 10px;
  border-radius: 2px;
  border: 1px solid;
  background: #EEE;
}
input[type='range']::-ms-track {
  box-sizing: border-box;
  width: 210px;
  height: 5px;
  border-radius: 2px;
  padding: 0px;
  background: #777;
  color: #777;
}
input[type='range']::-webkit-slider-thumb {
  box-sizing: border-box;
  padding: 0px;
  height: 20px;
  width: 10px;
  border-radius: 2px;
  border: 1px solid;
  margin-top: -8px;
  background: #EEE;
}
input[type='range']::-ms-thumb {
  box-sizing: border-box;
  padding: 0px;
  height: 20px;
  width: 10px;
  border-radius: 2px;
  border: 1px solid;
  background: #EEE;
}
input[type="range"]::-ms-fill-lower {
  background: transparent;
}
input[type='range']:focus {
  outline: none;
}
.field-wrapper rangeinput:after {
  position: absolute;
  color: #777;
  content: '20 40 60 80';
  padding: 40px;
  word-spacing: 20px;
  left: 0px;
  top: 0px;
  z-index: -1;
}
.field-wrapper rangeinput {
  padding: 0px;
  position: relative;
}

.field-wrapper rangeinput output{
  display: block;
  margin-top: 20px;
  color: #777;
}
.field-wrapper rangeinput output:before{
  content:"Selected Value: ";
  font-weight: bold;
}
.field-wrapper rangeinput body {
  font-family: Calibri, Arial;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="field-wrapper">
  <span class="handle ui-icon ui-icon-grip-dotted-vertical"></span>
  <input type="range" min="0" max="100" value="50" step="1" list="tickmarks" id="rangeInput">
  <span id="amount">50</span>
  <datalist id="tickmarks">
    <option value="0 to 20">0</option>
    <option>20</option>
    <option>40</option>
    <option>60</option>
    <option>80</option>
    <option>100</option>
  </datalist>
</div>


推荐阅读