jQuery UI 滑块标签 - 调整大小时遵循手柄
jQuery UI slider labels - follow handles on resize
我使用标签通过 jQuery UI 滑块显示句柄值。
如何在调整大小时保持这些值的位置?
这里是 fiddle:http://jsfiddle.net/kirkbross/vc8tumg9/3/
$(label).html(hours + ':' + minutes).position({
my: 'top center',
at: 'bottom center',
of: ui.handle,
offset: "0, 30"
});
为了加分...你能告诉我为什么我的初始值没有显示在正确的位置(如幻灯片上)吗?
双倍加分...你能告诉我除了手柄之外如何将幻灯片绑定到标签吗?
您可以使用 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);
}
});
我使用标签通过 jQuery UI 滑块显示句柄值。
如何在调整大小时保持这些值的位置?
这里是 fiddle:http://jsfiddle.net/kirkbross/vc8tumg9/3/
$(label).html(hours + ':' + minutes).position({
my: 'top center',
at: 'bottom center',
of: ui.handle,
offset: "0, 30"
});
为了加分...你能告诉我为什么我的初始值没有显示在正确的位置(如幻灯片上)吗?
双倍加分...你能告诉我除了手柄之外如何将幻灯片绑定到标签吗?
您可以使用 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);
}
});