简体   繁体   English

JQuery-UI滑块自动增量但不会触发幻灯片事件

[英]JQuery-UI Slider Auto Increments But Does Not Fire Slide Event

Here is the live demo . 这是现场演示 What I am trying to achieve is that the user can either manually use the slider handle to change the values shown in the highcharts chart or they can click play and it will auto-increment the slider's value and make the change in the chart. 我想要实现的是用户可以手动使用滑块手柄来更改高图表中显示的值,也可以单击播放,它会自动增加滑块的值并在图表中进行更改。 So far I the manual slider action works as designed. 到目前为止,我手动滑块动作按设计工作。 However, when I have the auto slide fired it does increment the slider's value/position but it does not fire the the slider.slide function that makes the changes to the chart. 但是,当我自动滑动时它会增加滑块的值/位置,但它不会触发对图表进行更改的slider.slide函数。 Probably something really simple but I cannot find it. 可能是非常简单的东西,但我找不到它。

Code for slider: 滑块代码:

$(function () {
    $("#slider").slider({
        value: 0,
        range: "month",
        min: 0,
        max: data1.length - 1,
        animate: true,
        slide: function (event, ui) {
            chartIncrement(this, ui.value);
        }
    });
});

Code for the auto-increment: 自动增量代码:

function scrollSlider() {
    var slideValue;
    slideValue = $("#slider").slider("value");
    if (slideValue >= 0) {
        if (slideValue == data1.length - 1) {
            slideValue = -1;
        }
        $("#slider").slider("value", slideValue + 1);
        console.log($("#slider").slider("value"));
        setTimeout(scrollSlider, 1000);
    }
}

$('#startSlider').click(function () {
    scrollSlider();
});

And here is the code that affects the change in the chart: 以下是影响图表变化的代码:

function updateChart(chart, value) {
    chart.series[0].setData([data1[value]]);
}
function chartIncrement(identifier, value) {
    $("#slider-val").text(Highcharts.dateFormat('%b %Y', data1[value][0]));
    updateChart(chart, value);
}

How can I have the auto-increment method also fire the slide function? 如何使用自动增量方法同时激活滑动功能? I thought if you changed the value regardless of method it would "do" work. 我想如果你改变了价值而不管它会“做”工作的方法。

According to the docs, use the change callback and not the slide callback (bolding mine): 根据文档,使用change回调而不是slide回调(bolding mine):

slide( event, ui )Type: slide slide(event,ui)类型:幻灯片

Triggered on every mouse move during slide. 滑动期间每次鼠标移动时触发。 The value provided in the event as ui.value represents the value that the handle will have as a result of the current movement. 事件中提供的值为ui.value表示句柄将由于当前移动而具有的值。 Canceling the event will prevent the handle from moving and the handle will continue to have its previous value. 取消事件将阻止句柄移动,句柄将继续具有其先前的值。


change( event, ui )Type: slidechange change(event,ui)类型:slidechange

Triggered after the user slides a handle, if the value has changed; 用户滑动手柄后触发,如果值已更改; or if the value is changed programmatically via the value method. 或者如果通过值方法以编程方式更改值。

Updated fiddle . 更新了小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM