views:

37

answers:

1

Hi everyone,

I'm trying to implement a page where there are 4 jQuery-UI sliders, and I want to make it so the combined total of all 4 sliders will never go over 400.

I don't mind which way this is implemented, it can be from a 0 start, and as soon as you change 1 slider, the remaining available total decreases or setting a slider past the maximum, decreases the values on the other sliders.

P.S. The sliders go in increments of 10.

All ideas & suggestions are welcome, and I've set up a jsFiddle if you'd like to test.

+1  A: 

Well here ya go:

var sliders = $("#sliders .slider");

sliders.each(function () {
    var value = parseInt($(this).text());
    var availableTotal = 400;

    $(this).empty().slider({
        value: 0,
        min: 0,
        max: 400,
        range: "max",
        step: 10,
        slide: function (event, ui) {
            // Current value
            $(this).siblings().text(ui.value);

            // Get current total
            var total = 0;

            sliders.not(this).each(function(){
                total += $(this).slider("option", "value");
            });

            // Need to do this because apparently jQ UI
            // does not update value until this event completes
            total += ui.value;

            var max = availableTotal - total;

            // Update each slider
            sliders.not(this).each(function(){
                var value = $(this).slider("option", "value");

                $(this).slider("option", "max", max + value);
                $(this).slider("option", "value", value);

                $(this).siblings().text(value + ' / ' + (max + value));
            });
        }
    });
});

A simple solution, I suppose.

Yi Jiang
That's awesome, thanks alot!
Marko