【问题标题】:jQuery Slider - Moving Sliders at the same timejQuery Slider - 同时移动滑块
【发布时间】:2016-01-20 15:00:36
【问题描述】:

我有如下三个滑块:-

<div class="slider" data-min="3800" data-max="30000" data-value="3848" data-step="500" data-target=".calc-deposit"></div>
<div class="slider" data-min="15400" data-max="120000" data-value="15393" data-step="2000" data-target=".calc-loan"></div> 
<div class="slider" data-min="57000" data-max="450000" data-value="57724" data-step="7500" data-target=".calc-mortgage"></div> 


$('.slider').each(function() {
    var $el = $(this);
    $el.slider({
        range: "max",
        min: $el.data('min'),
        max: $el.data('max'),
        value: $el.data('value'),
        step: $el.data('step'),
        slide: function(event, ui) {
            $($el.data('target')).html('£' + ui.value);

            // place code here which will execute when *any* slider moves
        }
    });
});

我想做的是不管你使用哪个滑块,它们都同时增加。

所有三个滑块都有不同的增量,因此它们需要相应地更新。

任何帮助将不胜感激。

Example

【问题讨论】:

  • 如果你关心增量,为什么初始值不在增量中?即3848 不是380030000 之间的500 增量?
  • 因为他们有不同的步骤/价值观,你应该有一个你必须遵循的规则。你有什么样的规则,假设第一个滑块在 4300 上,那么其他 2 个呢?

标签: jquery jquery-ui jquery-slider


【解决方案1】:

这是使用stopchange 事件的一种方式:-

$('.slider').each(function() {
  var $el = $(this);
  $el.slider({
    range: "max",
    min: $el.data('min'),
    max: $el.data('max'),
    value: $el.data('value'),
    step: $el.data('step'),
    stop: function(event, ui) {
      var percent = (100 / ($(this).data('max') - $(this).data('min'))) * $(this).slider('value');
      $('.slider').not(this).each(function() {
        $(this).slider('value', (($(this).data('max') - $(this).data('min')) / 100) * percent);
      });
    },
    change: function(event, ui) {
      $($el.data('target')).html('£' + ui.value);
    }
  });
});

FIDDLE

【讨论】:

    【解决方案2】:

    根据BG101的回答,这是最终结果:-

    jQuery('.slider').each(function() {
        var $el = jQuery(this);
        $el.slider({
            range: "max",
            min: $el.data('min'),
            max: $el.data('max'),
            value: $el.data('value'),
            step: $el.data('step'),
            slide: function(event, ui) {
                var percent = (100 / (jQuery(this).data('max') - jQuery(this).data('min'))) * jQuery(this).slider('value');
    
                jQuery('.slider').not(this).each(function() {
                    jQuery(this).slider(
                        'value', 
                        ((jQuery(this).data('max') - jQuery(this).data('min')) / 100) * percent
                    );
                });
                jQuery('.slider').each(function() {
                    var thisTarget = jQuery(this).data('target');
                    var thisValue = jQuery(this).slider('option','value');
    
                    jQuery(thisTarget+' span').html(thisValue);
                });
    
            },
        });
    });
    

    JSFIDDLE

    (只是想我会分享)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 2011-11-16
      • 2015-10-15
      • 2011-11-15
      • 2019-12-17
      相关资源
      最近更新 更多