使用范围滑块缓和然后再返回

时间:2015-05-22 14:46:54

标签: javascript angularjs ionic

我有一个范围滑块,我用来控制输入框中的值。我们在最大端使用的值可能非常高,因此我们希望在较大数字之前更快地进行更精细的控制。

为了尝试解决这个问题,我使用了一个缓动方程,而不是基于滑块值的时间。这非常适合在输入字段中设置文本。我遇到的问题现在又回到了另一个方向。我想让用户输入输入字段,并让滑块根据缓动方程移动到应该的位置。为此我需要一些如何向后工作,但我真的不知道如何转换函数。

到目前为止我have a codepen,我会非常感谢任何帮助或建议。

angular.module('ionicApp', ['ionic'])

.controller('MyCtrl', function($scope) {
  var min_amount = 10;
  var max_amount = 2000;
  var ease = function(t, b, c, d) {
    t /= d;
    return c*t*t + b;
  }

  $scope.transaction = {
    slider: 0,
    amount: 1
  }

 $scope.sliderUpdate = function() {
  var  quint = ease($scope.transaction.slider, min_amount, max_amount - min_amount, 100); console.log(quint);
   $scope.transaction.amount = quint;
 }

});

1 个答案:

答案 0 :(得分:0)

您可以使用以下功能。

  var reverseEase = function(x, b, c, d) {
    return d * Math.sqrt((x - b)/ c);
  }

此处x是缓和值。

工作代码段。



angular.module('ionicApp', ['ionic'])

.controller('MyCtrl', function($scope) {
  var min_amount = 10;
  var max_amount = 2000;
  var ease = function(t, b, c, d) {
    t /= d;
    return c * t * t + b;
  }

  var reverseEase = function(x, b, c, d) {
    return d * Math.sqrt((x - b) / c)
  }

  $scope.transaction = {
    slider: 0,
    amount: 1
  }

  $scope.transaction.reverseamount = 0;
  $scope.sliderUpdate = function() {
    var quint = ease($scope.transaction.slider, min_amount, max_amount - min_amount, 100);
    console.log(quint);
    $scope.transaction.amount = quint;

    $scope.transaction.reverseamount = reverseEase(quint, min_amount, max_amount - min_amount, 100);
  }

  $scope.transaction.reverseamount2 = reverseEase(10, min_amount, max_amount - min_amount, 100);
  $scope.transaction.valueChange = function(val) {
    if (!isNaN(val) && val > 9 && val < 2001) {
      $scope.transaction.reverseamount2 = reverseEase(val, min_amount, max_amount - min_amount, 100);
      $scope.errormsg = "";
    } else {
      $scope.errormsg = "invalid value";
    }
  }

  $scope.sliderUpdate2 = function(value) {
    $scope.transaction.inputValue = ease(value, min_amount, max_amount - min_amount, 100);
  }

});
&#13;
body {
  cursor: url('http://ionicframework.com/img/finger.png'), auto;
}
.errorColor {
  color: red;
}
&#13;
<html ng-app="ionicApp">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

  <title>Ionic Pull to Refresh</title>

  <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
  <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>

</head>

<body ng-controller="MyCtrl">

  <ion-header-bar class="bar-positive">
    <h1 class="title">Blank Starter</h1>
  </ion-header-bar>

  <ion-content>

    <input type="text" value="1" ng-model="transaction.amount" />

    <input type="range" min="0" max="100" step="0.01" value="0" id="input-topUp-range" ng-model="transaction.slider" ng-change="sliderUpdate();" />

    <br/>{{transaction.reverseamount}}
    <br/>
    <input type="range" min="0" max="100" step="0.01" value="0" id="input-topUp-range2" ng-model="transaction.reverseamount" />

    <hr/>Number(between 10 to 2000):
    <input ng-model="transaction.inputValue" ng-keyup="transaction.valueChange(transaction.inputValue)" />
    <br/>
    <input type="range" min="0" max="100" step="0.01" value="0" id="input-topUp-range3" ng-model="transaction.reverseamount2" ng-change="sliderUpdate2(transaction.reverseamount2);" />
    <span class="errorColor">{{errormsg}}</span>
  </ion-content>

</body>

</html>
&#13;
&#13;
&#13;

相关问题