gpt4 book ai didi

javascript - jQuery UI Slider - 仅在处理程序/ slider /标记之间更改背景颜色

转载 作者:行者123 更新时间:2023-11-29 19:26:02 25 4
gpt4 key购买 nike

我正在使用 jQuery UI slider 。我有多个范围设置为 false 的 hanldes。有没有办法为两个 handle / slider /标记之间的范围着色,无论你想怎么调用它们?我还没有找到解决方案。

这是我正在使用的代码/初始化。

    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 时,我的句柄消失了。我的想法是在两个 handle 上附加一个 div 并为该 div 的背景着色。虽然不适合我。

这是我最初的 slider 的样子:http://jsfiddle.net/5TTm4/3095/

我想在 handle 组之间涂色

最佳答案

您可以在 slider 内添加 div 并在移动 handle 时调整它们的大小。使用适当的 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 事件,部分原因是 slide 事件只在移动时触发,所以当你停止时,有片刻不更新。但也许如果您在其他事件上运行 resize color 会得到更好的结果。

参见 fiddle :http://jsfiddle.net/t4veqohy/1/

关于javascript - jQuery UI Slider - 仅在处理程序/ slider /标记之间更改背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30764463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com