您已经在“普拉”中存储了可用的点数。
每当用户调整滑块时,需要更改所有剩余的滑块,以便用户不能分配超过剩余的点数。您可以通过将.slider()MAX选项设置为滑块的当前值+剩余的可用点来完成此操作。
for (value in slidersValues) { var currentValue = slidersValues[value]; var newMax; //If max + available is > 1500, we set it to 1500, //otherwise we add the available points to the current values if ( currentValue + pula >= 1500) { newMax = 1500; } else { newMax = currentValue + pula; } //jQuery allows you to reset the 'max' value for a slider $('#'+value).slider( "option" , max , newMax ); }
您会发现调整滑块的最大值可能不是一个非常理想的解决方案。它将改变滑块上增量的比例,而不能很好地代表当前滑块值的整个部分。相反,如果所有其他滑块的当前总和加上此事件的值超过最大值,则拦截并阻止滑动事件。例如:
var maxSum = 1500; var $sliders = $(".slider").slider({ value: 0, min: 0, max: 1500, step: 10, slide: function(event, ui) { var sum = 0; // Don't add the value for this slider; // We need to add the new value represented by the slide event $(".slider").not(this).each(function() { sum += $(this).slider("value"); }); // Add the slide event value sum += ui.value; if (sum > maxSum) event.preventDefault(); else $(this).next().html("Value: " + ui.value); } });
如果用户以非常快的速度快速滑动,那么唯一容易出现的就是不会将值一直推到最大值。 在这里查看工作演示 。