简体   繁体   English

JQueryUI滑块-3个标签

[英]JQueryUI Slider - 3 Labels

I am working on a project that requires the user to select a hidden value from -10<=>10 我正在一个需要用户从-10 <=> 10中选择隐藏值的项目

I like the look and function of the JQueryUI widgets, and the increment slider would work if.... I can place 3 labels. 我喜欢JQueryUI小部件的外观和功能,并且如果...可以使用增量滑块。...我可以放置3个标签。

OptionA..................Neutral.....................OptionB 选项A ..................中性..................选项B

Their is a well-formed JSFiddle that has formatted numbers or ticks for each increment spot. 它们是格式良好的JSFiddle,已为每个增量点格式化了数字或刻度。 I would really just like it to have the 3 as illustrated above. 我真的很喜欢上面有3个。

http://jsfiddle.net/9y501okz/ http://jsfiddle.net/9y501okz/

$("#slider").slider({
    value: 4,
    min: 1,
    max: 7,
    step: 1
})
    .each(function () {
        //
        // Add labels to slider whose values 
        // are specified by min, max and whose
        // step is set to 1
        //
        // Get the options for this slider
        var opt = $(this).data().uiSlider.options;
        // Get the number of possible values
        var vals = opt.max - opt.min;
        // Space out values
        for (var i = 0; i <= vals; i++) {
            var el = $('<label>' + (i + 1) + '</label>').css('left', (i / vals * 100) + '%');
            $("#slider").append(el);
        }
    });

#slider label {
    position: absolute;
    width: 20px;
    margin-left: -10px;
    text-align: center;
    margin-top: 20px;
}

/* below is not necessary, just for style */
#slider {
    width: 50%;
    margin: 2em auto;
}

Thank you. 谢谢。

Your example is easily edited to achieve your desired results. 您的示例易于编辑,以实现所需的结果。

Change your slider initialization to use the following options: 更改滑块初始化以使用以下选项:

$( "#slider" ).slider({
    value: 2,
    min: 1,
    max: 3,
    step: 1
})

Your labels can be edited by adding a simple switch inside your loop: 您可以通过在循环内添加一个简单的开关来编辑标签:

for (var i = 0; i <= vals; i++) {
  var text = "";
  switch (i) {
      case 0:
          text = "OptionA";
          break;
      case 1:
          text = "Neutral";
          break;
      case 2:
          text = "OptionB";
          break;
  }
  var el = $('<label>'+text+'</label>').css('left',(i/vals*100)+'%');

  $( "#slider" ).append(el);

}

Example here 这里的例子

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

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