jQuery UI 滑块动画未更新 ui 值

jQuery UI slider animation not updating ui vValue

如何为 jQuery ui 滑块设置动画,并在页面加载时自动更新值。

当页面加载时,应该从左到右而不是向后动画,问题是当滑块开始动画时值没有更新

<!doctype html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
            <link rel="stylesheet" href="css/style.css">
            <link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">

        </head>
        <body>

            <p>
                <label for="amount">Minimum number of bedrooms:</label>
                <input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
            </p>
            <div id="slider-range-max"></div>
            <!-- Scripts -->
            <script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
            <script src="//code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
            <script>
            $(function() {
            $( "#slider-range-max" ).slider({
            range: "max",
            min: 0,
            max: 10000,
            step: 100,
            value: 0,
            animate: true,
            slide: function( event, ui ) {
            $( "#amount" ).val( ui.value );
            },
            change: function( event, ui ) {
            $( "#amount" ).val( ui.value );
            }
            });
            $( "#amount" ).val( $( "#slider-range-max" ).slider( "value" ) );
            $('.ui-slider-handle').animate({
                left: '0%'
            }, 1500, 'linear').animate({
                left: '50%'
            }, 1200, 'linear').animate({
                left: '40%'
            }, 1300, 'linear');
            });

            </script>
        </body>
    </html>

为了将初始动画期间的值变化同步到金额输入,您可以使用动画的步进事件和现在的值。如果您希望在用户更改值时每个动画步骤的数量发生变化,您将需要在幻灯片事件中执行类似的操作。

$(function() {
    var maxValue = 10000;

    $( "#slider-range-max" ).slider({
      range: "max",
      min: 0,
      max: maxValue,
      step: 100,
      value: 0,
      animate: true,
      slide: function( event, ui ) {
        $( "#amount" ).val( ui.value );
      },
      change: function( event, ui ) {
        $( "#amount" ).val( ui.value );
      }
    });

    $( "#amount" ).val( $( "#slider-range-max" ).slider( "value" ) );

    $('.ui-slider-handle')
      .animate({ left: '0%' }, 
      { 
        duration: 1500, 
        easing: 'linear', 
        step: function (now) { 
          if (now <= 0)
            return;

          $("#amount").val(Math.round((now / 100) * maxValue));
        }
      })
      .animate({ left: '50%' }, 
      { 
        duration: 1200, 
        easing: 'linear', 
        step: function (now) { 
          if (now <= 0)
            return;

          $("#amount").val(Math.round((now / 100) * maxValue));
        }
      })
      .animate({ left: '40%' }, 
      { 
        duration: 1300, 
        easing: 'linear', 
        step: function (now) { 
          if (now <= 0)
            return;

          $("#amount").val(Math.round((now / 100) * maxValue));
        }
      });
  });
});

要将值放入滑块中,几乎可以按照 jsfiddle 进行操作,除了还要将 .ui-slider-handle 文本更新放入初始动画中:

$(function() {
    var maxValue = 10000;

    $( "#slider-range-max" ).slider({
      range: "max",
      min: 0,
      max: maxValue,
      step: 100,
      value: 0,
      animate: true,
      slide: function( event, ui ) {
        $( "#amount" ).val( ui.value );

        $(this).find('.ui-slider-handle').text(ui.value);
      },
      change: function( event, ui ) {
        $( "#amount" ).val( ui.value );
      }
    });

    $( "#amount" ).val( $( "#slider-range-max" ).slider( "value" ) );

    $('.ui-slider-handle')
      .animate({ left: '0%' }, 
      { 
        duration: 1500, 
        easing: 'linear', 
        step: function (now) { 
          if (now <= 0)
          return;

          var currentVal = Math.round((now / 100) * maxValue));

          $("#amount").val(currentVal);
          $(this).text(currentVal);
        }
      })
      .animate({ left: '50%' }, 
      { 
        duration: 1200, 
        easing: 'linear', 
        step: function (now) { 
          if (now <= 0)
            return;

          var currentVal = Math.round((now / 100) * maxValue));

          $("#amount").val(currentVal);
          $(this).text(currentVal);
        }
      })
      .animate({ left: '40%' }, 
      { 
        duration: 1300, 
        easing: 'linear', 
        step: function (now) { 
          if (now <= 0)
            return;

          var currentVal = Math.round((now / 100) * maxValue));

          $("#amount").val(currentVal);
          $(this).text(currentVal);
        }
      });