jQuery UI滑块标签 - 跟随调整大小的句柄

时间:2015-03-05 20:17:32

标签: jquery jquery-ui slider position

我正在使用标签来显示jQuery UI滑块的句柄值。

如何在调整大小时保持这些值的位置?

这是一个小提琴:http://jsfiddle.net/kirkbross/vc8tumg9/3/

$(label).html(hours + ':' + minutes).position({
my: 'top center',
at: 'bottom center',
of: ui.handle,
offset: "0, 30"
});

为了额外的功劳......你能告诉我为什么我的初始值没有显示在正确的位置(如幻灯片上)吗?

对于双倍的额外信用......除了句柄之外,你能告诉我如何将幻灯片绑定到标签上吗?

1 个答案:

答案 0 :(得分:1)

您可以使用css添加标签...唯一的主要问题是获取初始值,因为create事件未提供ui变量(demo):

CSS

/* Add tooltips to slider handles */
 .ui-slider-handle:after {
    content : attr(data-value);
    position: absolute;
    bottom: 30px;
    left: -24px;
    min-width: 60px;
    height: 12px;
    background-color: #eee;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    border: #ddd 1px solid;
    color: #333;
    font: .7em/1em Arial, Sans-Serif;
    padding: 1px;
    text-align: center;
}
.ui-slider-handle:before {
    content:"";
    position: absolute;
    width: 0;
    height: 0;
    border-top: 8px solid #eee;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    top: -8px;
    left: 50%;
    margin-left: -8px;
    margin-top: -1px;
}

脚本

var initialValues = [180, 330, 1080, 1320],
    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);
};

$(".pct-slider").slider({
    min: 0,
    max: 1440,
    step: 15,
    range: false,
    values: initialValues,
    create: function (event, ui) {
        $.each( initialValues, function(i, v){
            updateValue({
                value: v,
                handle: $('.ui-slider-handle').eq(i) 
            });
        });
    },
    slide: function (event, ui) {
        updateValue(ui);
    }
});