代码之家  ›  专栏  ›  技术社区  ›  Pierre

包含多个允许节/时间段的javascript滑块

  •  1
  • Pierre  · 技术社区  · 7 年前

    我想在jquery/javascript滑块表单中实现一个时隙选择器。

    有一些滑块库在那里,如离子滑块,jqrangeslider等,但我不知道我会怎么做。看起来他们不支持多个“死区”。

    我希望用户能够在特定的一天内选择一个时间段(从和到)。为了选择日期,我实现了一个日期选择器,然后对于日期,我检索已经占用的插槽,例如:

    07h00 - Available
    07h30 - Available
    08h00 - Occupied
    08h30 - Occupied
    09h00 - Occupied
    09h30 - Available
    ...
    18h30 - Available
    19h00 - Available
    

    因此,范围选择器必须如下所示: enter image description here

    用户应该只能在可用区域(蓝色)中选择一个时区,并在“可用”区域之间拖动开始滑块,结束选择器将随之移动。可能有多个不可用区域(红色)。

    这是可能的图书馆已经有或这是一个滚动我自己的情况?

    我已经考虑过使用一堆复选框,然后选中开始和结束时间段之间的所有复选框,并禁用已经占用的时间段,但我认为这样的滑块在功能和视觉上更便于用户使用。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Chris Gunawardena    7 年前

    通过使用css将两个滑块叠加在一起,只需很少的努力就可以制作出一个双滑块。您需要监听这两个onchange事件,并在设置为死区时将滑块重置为上一个值或壁橱非死区。

    var deadZones = [[2,3], [6,7]];
    function showVal(input) {
        deadZones.forEach(([from, to]) => {
          // reset to old value if in dead zone
          if(from <= input.value && input.value <= to)
            input.value = input.oldValue;
        });
      input.oldValue = input.value;
      //console.log(input.id, input.value);
      displayValues();
    }
    
    function displayValues() {
      var a = $('#a').val();
      var b = $('#b').val();
      $('#slider-values').text(`Min: ${Math.min(a,b)} Max: ${Math.max(a,b)}`);
    }
    displayValues();
    html,body{
        margin: 0; padding: 0;
    }
    #a, #b{
      position: absolute;
      top: 30px;
      display: block;
      z-index: 100;
    }
    #b {
      top: 60px;
    }
    
    /* from: https://css-tricks.com/styling-cross-browser-compatible-range-inputs-css/ */
    input[type=range] {
      -webkit-appearance: none; /* Hides the slider so that custom slider can be made */
      width: 90%; /* Specific width is required for Firefox. */
      background: transparent; /* Otherwise white in Chrome */
      margin-left: 5%;
    }
    
    input[type=range]::-webkit-slider-thumb {
      -webkit-appearance: none;
      border: 1px solid #000000;
      height: 36px;
      width: 16px;
      border-radius: 3px;
      background: #ffffff;
      cursor: pointer;
    automatic */
      box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d; /* Add cool effects to your sliders! */
      position: relative;
    }
    input[type=range]#a::-webkit-slider-thumb {
       top: 100px;
    }
    input[type=range]#b::-webkit-slider-thumb {
       top: 70px;
    }
    
    .slider-bg {
      width: 100%;
      margin: 0; padding: 0;
      margin-left: 2.5%;
      position: relative;
      z-index: 1;
      top: 135px;
    }
    .slider-bg div {
      display: inline-block;
      width: 9%;
      margin: 0; padding: 0;
      text-align: center;
      border-top: 1px solid green;
      padding-top: 20px;
    }
    .slider-bg div.disabled {
      border-top: 1px solid red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <input id="a" type="range" min="1" max="10" value="1" oninput="showVal(this)" onchange="showVal(this)" />
    <input id="b" type="range" min="1" max="10" value="9" oninput="showVal(this)" onchange="showVal(this)"/>
    <hr />
    <div class="slider-bg">
      <div>1</div>
      <div class="disabled">2</div>
      <div class="disabled">3</div>
      <div>4</div>
      <div>5</div>
      <div class="disabled">6</div>
      <div class="disabled">7</div>
      <div>8</div>
      <div>9</div>
      <div>10</div>
    </div>
    <div id="slider-values"></div>
        2
  •  0
  •   Pierre    7 年前

    我决定实现一个带有自定义插槽的IonRangeSlider 05h30 19h30 . 一个单独的数组 used 我比较的时间段 onChange 事件。

    var slider = $("#timeslotSlider").ionRangeSlider({
        type: "double",
        grid: true,
        from: 1,
        from_value: "06h00",
        to: 2,
        to_value: "06h30",
        values: timeslotvalues,
        onChange: function (data) {
            timeslotSetSelectedText(data);
        }
    });
    
    var sliderdata = slider.data("ionRangeSlider");
    var dt = sliderdata.result.from_value != null ? sliderdata.result : sliderdata.options;
    
    timeslotSetSelectedText(dt);
    

    这个 timeslotSetSelectedText 函数将选定范围与 习惯于 插槽然后显示一条消息 "Available" "Overlaps Existing time-slot"

    相同的函数用于 Validate 将选定的插槽发送到服务器之前。

    推荐文章