代码之家  ›  专栏  ›  技术社区  ›  Hiren Vaghasiya

使用同一类的Jquery UI范围滑块出现问题

  •  1
  • Hiren Vaghasiya  · 技术社区  · 8 年前

    我在代码段中添加了多次使用同一类的Jquery UI范围滑块,它工作得很好,但第二个滑块并没有显示数据值。

    它是在更改滑块后显示,而不是在初始化时显示,因此请指导我在应用多范围滑块时所犯的错误

    $(document).ready(function() {
      var initialValues = [540, 1020],
        updateValue = function(ui) {
          var hours = Math.floor(ui.value / 60);
          var minutes = ui.value - (hours * 60);
    
          if (hours.length == 1) hours = '0' + hours;
          if (minutes.length == 1) minutes = '0' + minutes;
          if (minutes == 0) minutes = '00';
          if (hours >= 12) {
            if (hours == 12) {
              hours = hours;
              minutes = minutes + " PM";
            } else {
              hours = hours - 12;
              minutes = minutes + " PM";
            }
          } else {
            hours = hours;
            minutes = minutes + " AM";
          }
          if (hours == 0) {
            hours = 12;
            minutes = minutes;
          }
          $(ui.handle).attr('data-value', hours + ':' + minutes);
        };
    
      $(".slider-range").slider({
        min: 0,
        max: 1440,
        step: 15,
        range: true,
        values: [540, 1020],
        create: function(event, ui) {
          $.each(initialValues, function(i, v) {
            updateValue({
              value: v,
              handle: $('.ui-slider-handle').eq(i)
            });
          });
        },
        slide: function(event, ui) {
          updateValue(ui);
        }
      });
    });
    .time-range {
      padding-top: 35px;
    }
    
    *:focus {
      outline: 0;
    }
    
    .ui-slider-horizontal {
      height: 8px;
      background: #D7D7D7;
      border: 1px solid #BABABA;
      box-shadow: 0 1px 0 #FFF, 0 1px 0 #CFCFCF inset;
      clear: both;
      margin: 8px 0;
      -webkit-border-radius: 6px;
      -moz-border-radius: 6px;
      -ms-border-radius: 6px;
      -o-border-radius: 6px;
      border-radius: 6px;
    }
    
    .ui-slider {
      position: relative;
      text-align: left;
    }
    
    .ui-slider-horizontal .ui-slider-range {
      top: -1px;
      height: 100%;
    }
    
    .ui-slider .ui-slider-range {
      position: absolute;
      z-index: 1;
      height: 8px;
      font-size: .7em;
      display: block;
      border: 1px solid #424453;
      box-shadow: 0 1px 0 #edeef7 inset;
      -moz-border-radius: 6px;
      -webkit-border-radius: 6px;
      -khtml-border-radius: 6px;
      border-radius: 6px;
    }
    
    .ui-slider .ui-slider-handle {
      background-color: #424453;
      width: 18px;
      height: 18px;
    }
    
    .ui-slider .ui-slider-handle {
      position: absolute;
      z-index: 2;
      width: 18px;
      height: 18px;
      cursor: default;
      border: none;
      cursor: pointer;
      background: transparent url(https://dummyimage.com/4x20/000/fff.png) 50% 50% no-repeat;
    }
    
    .ui-slider-horizontal .ui-slider-handle {
      top: -.5em;
      margin-left: -.7em;
    }
    
    .ui-slider-handle:after {
      content: attr(data-value);
      position: absolute;
      bottom: -24px;
      left: -20px;
      min-width: 60px;
      font-size: 10px;
      height: 20px;
      color: #333;
      padding: 1px;
      text-align: center;
    }
    
    .ui-slider a:focus {
      outline: none;
    }
    
    #slider-range {
      width: 95%;
      margin: 0px;
    }
    
    .time-range {
      width: 100%;
      margin: 10px 0px;
      display: block;
    }
    
    .time-slider-label {
      display: inline-block;
    }
    
    .sliders_step1 {
      width: auto;
    }
    
    #time-range label {
      margin-top: 10px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
    <div class="time-range">
      <div class="sliders_step1">
        <div class="slider-range">
        </div>
      </div>
    </div>
    
    <div class="time-range">
      <div class="sliders_step1">
        <div class="slider-range">
        </div>
      </div>
    </div>
    1 回复  |  直到 8 年前
        1
  •  2
  •   KL_    8 年前

    你需要找到 .slider-range 首先创建滑块,然后搜索 .ui-slider-handle 在里面。
    你可以找到哪一个 .index() : $(".slider-range").index(this)

    $(document).ready(function() {
      var initialValues = [540, 1020],
        updateValue = function(ui) {
          var hours = Math.floor(ui.value / 60);
          var minutes = ui.value - (hours * 60);
    
          if (hours.length == 1) hours = '0' + hours;
          if (minutes.length == 1) minutes = '0' + minutes;
          if (minutes == 0) minutes = '00';
          if (hours >= 12) {
            if (hours == 12) {
              hours = hours;
              minutes = minutes + " PM";
            } else {
              hours = hours - 12;
              minutes = minutes + " PM";
            }
          } else {
            hours = hours;
            minutes = minutes + " AM";
          }
          if (hours == 0) {
            hours = 12;
            minutes = minutes;
          }
          
          $(ui.handle).attr('data-value', hours + ':' + minutes);
        };
    
      $(".slider-range").slider({
        min: 0,
        max: 1440,
        step: 15,
        range: true,
        values: [540, 1020],
        create: function(event, ui) {
          var index = $(".slider-range").index(this);
          $.each(initialValues, function(i, v) {
            updateValue({
              value: v,
              handle: $('.slider-range').eq(index).find('.ui-slider-handle').eq(i)
            });
          });
        },
        slide: function(event, ui) {
          updateValue(ui);
        }
      });
    });
    .time-range {
      padding-top: 35px;
    }
    
    *:focus {
      outline: 0;
    }
    
    .ui-slider-horizontal {
      height: 8px;
      background: #D7D7D7;
      border: 1px solid #BABABA;
      box-shadow: 0 1px 0 #FFF, 0 1px 0 #CFCFCF inset;
      clear: both;
      margin: 8px 0;
      -webkit-border-radius: 6px;
      -moz-border-radius: 6px;
      -ms-border-radius: 6px;
      -o-border-radius: 6px;
      border-radius: 6px;
    }
    
    .ui-slider {
      position: relative;
      text-align: left;
    }
    
    .ui-slider-horizontal .ui-slider-range {
      top: -1px;
      height: 100%;
    }
    
    .ui-slider .ui-slider-range {
      position: absolute;
      z-index: 1;
      height: 8px;
      font-size: .7em;
      display: block;
      border: 1px solid #424453;
      box-shadow: 0 1px 0 #edeef7 inset;
      -moz-border-radius: 6px;
      -webkit-border-radius: 6px;
      -khtml-border-radius: 6px;
      border-radius: 6px;
    }
    
    .ui-slider .ui-slider-handle {
      background-color: #424453;
      width: 18px;
      height: 18px;
    }
    
    .ui-slider .ui-slider-handle {
      position: absolute;
      z-index: 2;
      width: 18px;
      height: 18px;
      cursor: default;
      border: none;
      cursor: pointer;
      background: transparent url(https://dummyimage.com/4x20/000/fff.png) 50% 50% no-repeat;
    }
    
    .ui-slider-horizontal .ui-slider-handle {
      top: -.5em;
      margin-left: -.7em;
    }
    
    .ui-slider-handle:after {
      content: attr(data-value);
      position: absolute;
      bottom: -24px;
      left: -20px;
      min-width: 60px;
      font-size: 10px;
      height: 20px;
      color: #333;
      padding: 1px;
      text-align: center;
    }
    
    .ui-slider a:focus {
      outline: none;
    }
    
    #slider-range {
      width: 95%;
      margin: 0px;
    }
    
    .time-range {
      width: 100%;
      margin: 10px 0px;
      display: block;
    }
    
    .time-slider-label {
      display: inline-block;
    }
    
    .sliders_step1 {
      width: auto;
    }
    
    #time-range label {
      margin-top: 10px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
    <div class="time-range">
      <div class="sliders_step1">
        <div class="slider-range">
        </div>
      </div>
    </div>
    
    <div class="time-range">
      <div class="sliders_step1">
        <div class="slider-range">
        </div>
      </div>
    </div>
    推荐文章