jQuery UI 滑块 - 仅在 handlers/sliders/markers 之间更改背景颜色
jQuery UI Slider - Change background color ONLY between handlers/sliders/markers
我正在使用 jQuery UI 滑块。我有多个范围设置为 false 的 hanldes。有没有办法为两个 handles/sliders/markers 之间的范围着色,无论你想怎么称呼它们?我还没有找到解决办法。
这是我正在使用的code/initialization。
var initialValues = [180, 435, 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;
}
//console.log(ui.handle)
$(ui.handle).attr('data-value', hours + ':' + minutes);
};
var timeLabels = ["12:00 a.m","1:00 a.m","2:00 a.m","3:00 a.m","4:00 a.m","5:00 a.m","6:00 a.m","7:00 a.m","8:00 a.m","9:00 a.m","10:00 a.m","11:00 a.m",
"12:00 p.m","1:00 p.m","2:00 p.m","3:00 p.m","4:00 p.m","5:00 p.m","6:00 p.m","7:00 p.m","8:00 p.m","9:00 p.m","10:00 p.m","11:00 p.m", "12:00 p.m"];
(function ($) {
$.widget('my-namespace.customSlider', $.ui.slider, {
options: {
ticks: false
},
// Called when the slider is instantiated.
_create: function() {
// Call the orginal constructor, creating the slider normally.
this._super();
// If the "ticks" option is false or the "step" option is
// less than 5, there's nothing to do.
if ( !this.options.ticks || this.options.step < 5 ) {
return;
}
// Setup some variables for rendering the tick marks below the slider.
var cnt = this.options.min,
background = this.element.css( "border-color" ),
left;
while ( cnt <= this.options.max ) {
// Compute the "left" CSS property for the next tick mark.
left = ( cnt / this.options.max * 100 ).toFixed( 2 ) + "%";
// Creates the tick div, and adds it to the element. It adds the
// "ui-slider-tick" class, which has common properties for each tick.
// It also applies the computed CSS properties, "left" and "background".
//console.log($("</div>"))
$( "<div/>" ).addClass( "ui-slider-tick" )
.appendTo( this.element )
.css( { left: left, background: background } );
cnt += this.options.step;
}
console.log(this.element[0].id)
cnt = this.options.min
var i = 0;
while (cnt <= this.options.max) {
//console.log(timeLabels[i])
$($(".pct-slider#" + this.element[0].id).find('.ui-slider-tick')[cnt]).append("<div class='tick-labels'>" + timeLabels[i] + "</div>");
cnt = cnt + 4;
i++;
}
//$(".pct-slider#" + sliders[0]).find('.ui-slider-tick').find('.tick-labels').hide()
},
addValue: function( val ) {
this.options.values.push(val);
console.log(val)
var time = convertToTime(val)
console.log(time)
this._refresh();
$($(".ui-slider-handle").last()).attr('data-value', time)
},
removeValue: function( ) {
if (this.options.values.length > 1) {
this.options.values.pop( );
this._refresh();
}
}
});
})(jQuery);
var sliders =["mondaySlider", "tuesdaySlider","wednesdaySlider","thursdaySlider","fridaySlider","saturdaySlider","sundaySlider"];
$(".pct-slider#" + sliders[0])
.customSlider({
min: 0,
max: 1440,
step: 15,
range: false,
ticks: true,
values: initialValues,
create: function (event, ui) {
$.each( initialValues, function(i, v){
updateValue({
value: v,
handle: $(".pct-slider#" + sliders[0]).find('.ui-slider-handle').eq(i)
});
});
},
slide: function (event, ui) {
var handleIndex = $('a', event.target).index(ui.handle),
curr = ui.values[handleIndex],
next = ui.values[handleIndex + 1] - 15,
prev = ui.values[handleIndex - 1] + 15;
if (curr > next || curr < prev) {
return false;
}
updateValue(ui);
//positionSelects();
}
});
我试图将 div 附加到我的句柄上,但是当我附加 div 时,我的句柄消失了。我的想法是在两个手柄上附加一个 div 并为 div 的背景着色。虽然不适合我。
这是我的初始滑块的 fiddle 样子:http://jsfiddle.net/5TTm4/3095/
我想在手柄组之间涂色
您可以在滑块内添加 div
并在移动手柄时调整它们的大小。使用适当的 css 它会产生您所描述的效果。您可能需要稍微调整一下,但这应该会给您一些想法:
HTML
//You add divs inside your slider, you need four, the last region will be
/background of slider
<div class="pct-slider" id="mondaySlider">
<div class="color-region"></div>
<div class="color-region"></div>
<div class="color-region"></div>
<div class="color-region"></div>
</div>
CSS
//Make your divs relative and give them color
.color-region
{
position: relative;
height: 100%;
float: left;
}
.color-region:nth-child(1)
{
background-color: blue;
}
.color-region:nth-child(1)
{
background-color: blue;
}
.color-region:nth-child(2)
{
background-color: red;
}
.color-region:nth-child(3)
{
background-color: yellow;
}
.color-region:nth-child(4)
{
background-color: green;
}
JS
function resize_colors() {
//you start at 0
var cur_pos = 0;
$(".ui-slider-handle").each(function (i) {
//for each handle you check position and set width of corresponding color div
$('.color-region').eq(i).css('width', $(this).position().left - cur_pos)
//update cur_pos to calculate next color width
cur_pos = $(this).position().left;
})
}
你会看到它并没有完全跟随slide
事件,部分原因是滑动事件只有在移动时才会触发,所以当你停止时,有片刻没有更新。但是,如果您 运行 调整其他事件的颜色大小,可能会得到更好的结果。
参见fiddle:http://jsfiddle.net/t4veqohy/1/
我正在使用 jQuery UI 滑块。我有多个范围设置为 false 的 hanldes。有没有办法为两个 handles/sliders/markers 之间的范围着色,无论你想怎么称呼它们?我还没有找到解决办法。
这是我正在使用的code/initialization。
var initialValues = [180, 435, 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;
}
//console.log(ui.handle)
$(ui.handle).attr('data-value', hours + ':' + minutes);
};
var timeLabels = ["12:00 a.m","1:00 a.m","2:00 a.m","3:00 a.m","4:00 a.m","5:00 a.m","6:00 a.m","7:00 a.m","8:00 a.m","9:00 a.m","10:00 a.m","11:00 a.m",
"12:00 p.m","1:00 p.m","2:00 p.m","3:00 p.m","4:00 p.m","5:00 p.m","6:00 p.m","7:00 p.m","8:00 p.m","9:00 p.m","10:00 p.m","11:00 p.m", "12:00 p.m"];
(function ($) {
$.widget('my-namespace.customSlider', $.ui.slider, {
options: {
ticks: false
},
// Called when the slider is instantiated.
_create: function() {
// Call the orginal constructor, creating the slider normally.
this._super();
// If the "ticks" option is false or the "step" option is
// less than 5, there's nothing to do.
if ( !this.options.ticks || this.options.step < 5 ) {
return;
}
// Setup some variables for rendering the tick marks below the slider.
var cnt = this.options.min,
background = this.element.css( "border-color" ),
left;
while ( cnt <= this.options.max ) {
// Compute the "left" CSS property for the next tick mark.
left = ( cnt / this.options.max * 100 ).toFixed( 2 ) + "%";
// Creates the tick div, and adds it to the element. It adds the
// "ui-slider-tick" class, which has common properties for each tick.
// It also applies the computed CSS properties, "left" and "background".
//console.log($("</div>"))
$( "<div/>" ).addClass( "ui-slider-tick" )
.appendTo( this.element )
.css( { left: left, background: background } );
cnt += this.options.step;
}
console.log(this.element[0].id)
cnt = this.options.min
var i = 0;
while (cnt <= this.options.max) {
//console.log(timeLabels[i])
$($(".pct-slider#" + this.element[0].id).find('.ui-slider-tick')[cnt]).append("<div class='tick-labels'>" + timeLabels[i] + "</div>");
cnt = cnt + 4;
i++;
}
//$(".pct-slider#" + sliders[0]).find('.ui-slider-tick').find('.tick-labels').hide()
},
addValue: function( val ) {
this.options.values.push(val);
console.log(val)
var time = convertToTime(val)
console.log(time)
this._refresh();
$($(".ui-slider-handle").last()).attr('data-value', time)
},
removeValue: function( ) {
if (this.options.values.length > 1) {
this.options.values.pop( );
this._refresh();
}
}
});
})(jQuery);
var sliders =["mondaySlider", "tuesdaySlider","wednesdaySlider","thursdaySlider","fridaySlider","saturdaySlider","sundaySlider"];
$(".pct-slider#" + sliders[0])
.customSlider({
min: 0,
max: 1440,
step: 15,
range: false,
ticks: true,
values: initialValues,
create: function (event, ui) {
$.each( initialValues, function(i, v){
updateValue({
value: v,
handle: $(".pct-slider#" + sliders[0]).find('.ui-slider-handle').eq(i)
});
});
},
slide: function (event, ui) {
var handleIndex = $('a', event.target).index(ui.handle),
curr = ui.values[handleIndex],
next = ui.values[handleIndex + 1] - 15,
prev = ui.values[handleIndex - 1] + 15;
if (curr > next || curr < prev) {
return false;
}
updateValue(ui);
//positionSelects();
}
});
我试图将 div 附加到我的句柄上,但是当我附加 div 时,我的句柄消失了。我的想法是在两个手柄上附加一个 div 并为 div 的背景着色。虽然不适合我。
这是我的初始滑块的 fiddle 样子:http://jsfiddle.net/5TTm4/3095/
我想在手柄组之间涂色
您可以在滑块内添加 div
并在移动手柄时调整它们的大小。使用适当的 css 它会产生您所描述的效果。您可能需要稍微调整一下,但这应该会给您一些想法:
HTML
//You add divs inside your slider, you need four, the last region will be
/background of slider
<div class="pct-slider" id="mondaySlider">
<div class="color-region"></div>
<div class="color-region"></div>
<div class="color-region"></div>
<div class="color-region"></div>
</div>
CSS
//Make your divs relative and give them color
.color-region
{
position: relative;
height: 100%;
float: left;
}
.color-region:nth-child(1)
{
background-color: blue;
}
.color-region:nth-child(1)
{
background-color: blue;
}
.color-region:nth-child(2)
{
background-color: red;
}
.color-region:nth-child(3)
{
background-color: yellow;
}
.color-region:nth-child(4)
{
background-color: green;
}
JS
function resize_colors() {
//you start at 0
var cur_pos = 0;
$(".ui-slider-handle").each(function (i) {
//for each handle you check position and set width of corresponding color div
$('.color-region').eq(i).css('width', $(this).position().left - cur_pos)
//update cur_pos to calculate next color width
cur_pos = $(this).position().left;
})
}
你会看到它并没有完全跟随slide
事件,部分原因是滑动事件只有在移动时才会触发,所以当你停止时,有片刻没有更新。但是,如果您 运行 调整其他事件的颜色大小,可能会得到更好的结果。
参见fiddle:http://jsfiddle.net/t4veqohy/1/