Is it possible to have a fixed, static range on a one-handle jQuery UI slider?

和自甴很熟 提交于 2019-12-12 12:59:13

问题


Essentially what I've got going is a single-handle jQuery UI slider for a bunch of dates. The date range chosen is ALWAYS two weeks. What I'd like to do is just show a range (the range doesn't have to be functional - just displayed) from the start date of the two week range (which is the date the handle resides on), two weeks forward. This range is fixed.

For example, if the handle is on Jan 1st, I'd like a range to extend to Jan 14th. If the slider moves to Jan 3rd, I'd like the range to move with it to end on the 16th.

Does anyone know if this is possible?


回答1:


The jQuery UI slider wasn't designed with that in mind, but you can handle the slide and stop events of the slider yourself to reset the range values however you like.

http://docs.jquery.com/UI/Slider

This doesn't address all of your requirements by any means, but it's a start.

$('#myslider').slider({
    min:1,
    max:31,
    values:[1, 14],
    step:1,
    range:true,
    slide:function(event, ui) {
        var s = $(this);
        var values = s.slider('option', 'values');

        if(ui.handle.nextElementSibling == null) // max slider
            values[0] = values[1] - 14;
        else // min slider
            values[1] = values[0] + 14;

        s.slider('option', 'values', values);
    },
    stop:function() {
        var s = $(this);
        var min = s.slider('option', 'min'),
            max = s.slider('option', 'max'),
            values = s.slider('option', 'values');

        if(values[0] <= min)
            values = [min, min + 14];
        if(values[1] >= max)
            values = [max - 14, max];

        s.slider('option', 'values', values);
    }
});


来源:https://stackoverflow.com/questions/8105088/is-it-possible-to-have-a-fixed-static-range-on-a-one-handle-jquery-ui-slider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!