简体   繁体   English

jQuery UI范围滑块工具提示

[英]jQuery UI range slider tooltips

How can I get my jQuery UI range slider to display tooltips on top of the markers like so: 我怎样才能使jQuery UI范围滑块在标记顶部显示工具提示,如下所示:

在此处输入图片说明

 <span class="tooltip"></span> <div id="slider-range"></div>
 <button id="reset" type="button" class="btn btn-success" onclick="reset_slider('#slider-range')">Reset</button>

Here is the script so far: 到目前为止,这是脚本:

$(document).ready(function() {
     $("#slider-range").slider({
         min: 0,
         max: 1000,
         step: 1,
         range: true,
         values: [0, 1000],
         slide: function(event, ui) {
             for (var i = 0; i < ui.values.length; ++i) {
                 $("input.sliderValue[data-index=" + i + "]").val(ui.values[i]);
             }
         }
     });

     $("input.sliderValue").change(function() {
         var $this = $(this);
         $("#slider-range").slider("values", $this.data("index"), $this.val());

     });
 });

Here is how it looks right now, all its missing is the tooltips on top. 这就是现在的样子,它缺少的只是顶部的工具提示。 在此处输入图片说明

You need to combine two examples: 您需要结合两个示例:

Working example: https://jsfiddle.net/Twisty/c90ee4xe/ 工作示例: https//jsfiddle.net/Twisty/c90ee4xe/

HTML 的HTML

<div class="wrapper">
  <div id="slider-range">
  </div>
  <a href="" id="reset" class="btn btn-success">Reset</a>
</div>

CSS 的CSS

.wrapper {
  display: block;
  padding: 3px;
  margin-top: 20px;
}

#slider-range {
  width: 75%;
  margin: 20px;
}

#slider-range .ui-slider-handle {
  background: #0e6;
  border-radius: 6px;
}

#slider-range .ui-slider-handle.ui-state-active {
  border: #093;
}

#slider-range .ui-slider-range {
  background: #093;
}

.ui-tooltip,
.arrow:after {
  background: #000;
}

.ui-tooltip {
  color: #fff;
  border: 0;
  border-radius: 10px;
  box-shadow: none;
}

.arrow {
  width: 70px;
  height: 16px;
  overflow: hidden;
  position: absolute;
  left: 50%;
  margin-left: -35px;
  bottom: -16px;
}

.arrow.top {
  top: -16px;
  bottom: auto;
}

.arrow.left {
  left: 20%;
}

.arrow:after {
  content: "";
  position: absolute;
  left: 20px;
  top: -20px;
  width: 25px;
  height: 25px;
  box-shadow: 6px 5px 9px -9px black;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}

.arrow.top:after {
  bottom: -20px;
  top: auto;
}

#reset {
  color: #fff;
  background: #090;
  border-radius: 6px;
  font-family: verdana;
  font-weight: bold;
  display: inline-block;
  padding: .25em;
  text-decoration: none;
}

JavaScript 的JavaScript

$(document).ready(function() {
  $("#slider-range").slider({
    min: 0,
    max: 1000,
    step: 1,
    range: true,
    values: [0, 1000],
    slide: function(event, ui) {
      var low, high;
      low = $(this).slider("values", 0);
      high = $(this).slider("values", 1);
      $(this).tooltip("option", "content", low + ":" + high);
    }
  });

  $("#slider-range").tooltip({
    items: ".ui-slider",
    content: "0:1000",
    position: {
      my: "center bottom-20",
      at: "center top",
      using: function(position, feedback) {
        $(this).css(position);
        $("<div>")
          .addClass("arrow")
          .addClass(feedback.vertical)
          .addClass(feedback.horizontal)
          .appendTo(this);
      }
    }
  });

  $("#reset").click(function(e) {
    e.preventDefault();
    $("#slider-range").slider("values", [0, 1000]).tooltip("option", "content", "0:1000").tooltip("close");
  });
});

For the Slider, we do most everything the same. 对于Slider,我们所做的大多数事情都一样。 Just in slide we update the content of the tooltip instead of fields. 仅在slide我们更新工具提示的content而不是字段。

For this example, we're only using 1 tooltip on 1 slider. 对于此示例,我们仅在1个滑块上使用1个工具提示。 If you have multiple sliders, see: http://jqueryui.com/tooltip/#custom-content It will be more complex. 如果您有多个滑块,请访问: http : //jqueryui.com/tooltip/#custom-content它将更加复杂。 If you have a smaller number of sliders, you can just make a tooltip for each. 如果滑块数量较少,则只需为每个滑块制作一个工具提示。 Otherwise it'll need the item option set and some ways to tie specific sliders to their tooltip. 否则,将需要设置item选项以及一些将特定滑块绑定到其工具提示的方法。

The tooltip is pretty straight forward. 工具提示非常简单。 We'll set the content to our intial value since we know it will be changed later. 我们将content设置为初始值,因为我们知道以后会对其进行更改。 We also set the position that will be fluid. 我们还设置了流动的position This helps move the tooltip above or below depending on the feedback and we style the arrow to correspond. 这有助于根据feedback在上方或下方移动工具提示,并且我们将箭头样式设置为对应。

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

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