JQuery-ui slider - How to stop two sliders from controlling each other

This refers to the earlier question.

The problem is that everyone slidercontrols the other. This leads to feedback.

How can I stop him?

$(function() {
    $("#slider").slider({ slide: moveSlider2 });
    $("#slider1").slider({ slide: moveSlider1 });
    function moveSlider2( e, ui ) 
    {
        $('#slider1').slider( 'moveTo', Math.round(ui.value) );
    }

    function moveSlider1( e, ui ) 
    {
        $('#slider').slider( 'moveTo', Math.round(ui.value) );
    }
});
+3
source share
5 answers

This is a kind of hack, but it works:

$(function () {
    var slider = $("#slider");
    var slider1 = $("#slider1");
    var sliderHandle = $("#slider").find('.ui-slider-handle');
    var slider1Handle = $("#slider1").find('.ui-slider-handle');

    slider.slider({ slide: moveSlider1 });
    slider1.slider({ slide: moveSlider });

    function moveSlider( e, ui ) {
        sliderHandle.css('left', slider1Handle.css('left'));
    }

    function moveSlider1( e, ui ) {
        slider1Handle.css('left', sliderHandle.css('left'));
    }
});

Basically, you avoid feedback by directly manipulating css without triggering a slide event.

+2
source

You can save var CurrentSlider = 'slider';

on mousedown on any of the sliders, you set the CurrentSlider value to this slider,

moveSlider (...) , CurrentSlider, , ( )

+1

You can simply specify an optional parameter for your functions moveSlider1and moveSlider2that, when set to true, suppress recursion.

0
source

A simpler approach, which is a kind of hybrid of the answers above:

    var s1 = true;
    var s2 = true;
    $('#slider').slider({
        handle: '.slider_handle',
        min: -100,
        max: 100,
        start: function(e, ui) {
        },
        stop: function(e, ui) { 
        },
        slide: function(e, ui) {
            if(s1)
            {
                s2 = false;
                $('#slider1').slider("moveTo", ui.value);
                s2 = true;
            }
        }
    });


    $("#slider1").slider({ 
        min: -100, 
        max: 100,
        start: function(e, ui) {
        },
        stop: function(e, ui) { 
        },
        slide: function(e, ui) {
            if(s2)
            {
                s1 = false;
                $('#slider').slider("moveTo", ui.value);
                s1 = true;
            }
        }
        });

});
0
source

Tried this now and all the answers are not working, possibly due to changes in jquery ui.

Badri solution works if you replace

$('#slider').slider("moveTo", ui.value);

with

$('#slider').slider("option", "value", ui.value);
0
source

Source: https://habr.com/ru/post/1699631/


All Articles