`Moving smoothly with a slider and then reversing direction`

I have implemented a range slider to control the value in an input field. The values can vary greatly, so I needed finer control for lower numbers that gradually expands for larger numbers.

To address this issue, I utilized an easing equation based on the slider value rather than time. This successfully sets the text in the input field. However, my current challenge lies in reversing this process. I want users to be able to type into the input field and have the slider move accordingly based on the easing equation. Unfortunately, I am unsure of how to reverse and convert the function for this purpose.

You can view what I have done so far on CodePen. Any assistance or suggestions would be greatly appreciated.

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;
 }

});

Answer №1

To achieve this, you can utilize the function provided below.

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

where x represents the ease value.

Here is a functional example:

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 customFunction = 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 = customFunction(quint, min_amount, max_amount - min_amount, 100);
  }

  $scope.transaction.reverseamount2 = customFunction(10, min_amount, max_amount - min_amount, 100);
  $scope.transaction.valueChange = function(val) {
    if (!isNaN(val) && val > 9 && val < 2001) {
      $scope.transaction.reverseamount2 = customFunction(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);
  }

});
body {
  cursor: url('http://ionicframework.com/img/finger.png'), auto;
}
.errorColor {
  color: red;
}
<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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

There was a hiccup during the execution of the npm subprocess

I encountered an issue while attempting to initiate a new ionic project. My troubleshooting steps included reinstalling npm, node, and ionic, as well as cleaning the npm cache using the commands: npm cache verify Error code: npm WARN deprecated [ ...

Unexpected behavior occurs when ajax is added to an array for jQuery promise results

In my code, I have an each loop that makes individual AJAX requests and stores them in an array like this: var promises = []; $items.each(function(k, v) { promises.push( $.ajax({ url: ..., .... }) ); }); $.when.app ...

Rails successfully processed the request with a 200 OK status code, however, the $.ajax() function is triggering the 'error' callback instead of the 'success' callback

In my controller, the ajax request is handled as shown below (simplified): def create ... @answer = Answer.new(video: video_file) if @answer.save render json: @answer.video.url else ... end end This is how I have defined my ajax function: $.aja ...

Measuring data entries within JSON array utilizing JavaScript and Postman

A specific component is returning two records: { "value": [ { "ID": 5, "Pupil": 1900031265, "Offer": false, }, { "ID": 8, "Pupil": 1900035302, "Offer": false, "OfferDetail": "" } ] } My task i ...

Perform an action in jQuery when a class is modified

I am trying to create an event that will trigger when a specific element receives a class, and also when that class is removed. For example: <button id="my_butt_show">showtime</button> <button id="my_butt_hide">hide me</button> &l ...

A windows application developed using either Javascript or Typescript

Can you provide some suggestions for developing Windows applications using Javascript, Typescript, and Node.js? ...

Learn how to incorporate an input field into a form using the same class name

I am facing a challenging JavaScript issue that I have been struggling to resolve. My predicament involves a dynamically formed table with form fields. Here is the code snippet in question: var table = document.getElementById("table"); var row = table. ...

What is the best way to generate dynamic components on the fly in ReactJS?

Could you please guide me on: Techniques to dynamically create react components, such as from simple objects? Is it possible to implement dynamic creation in JSX as well? Does react provide a method to retrieve a component after its creation, maybe b ...

Navigating following a JQuery AJAX request in PHP

After clicking the login button, I utilize JQuery's POST method to retrieve data from login.php to check if the login was successful. If it fails (no user found), the appropriate message is displayed. However, when attempting to redirect a user (whic ...

After upgrading to version 4.0.0 of typescript-eslint/parser, why is eslint having trouble recognizing JSX or certain react @types as undefined?"

In a large project built with ReactJs, the eslint rules are based on this specific eslint configuration: const DONT_WARN_CI = process.env.NODE_ENV === 'production' ? 0 : 1 module.exports = { ... After upgrading the library "@typescript-es ...

The functionality of "subscribe()" is outdated if utilized with "of(false)"

My editor is flagging the usage of of as deprecated. How can I resolve this issue and get it working with of? public save(): Observable<ISaveResult> | Observable<boolean> { if (this.item) { return this.databaseService.save(this.user ...

Generating div elements of varying colors using a combination of Jinja templating and JavaScript loop

Utilizing jinja and javascript in my template, I am creating multiple rows of 100 boxes where the color of each box depends on the data associated with that row. For instance, if a row in my dataset looks like this: year men women 1988 60 40 The co ...

NodeJS refuses to import a file that is not compatible with its structure

My website has two important files: firebase.js gridsome-server.js The firebase.js file contains JavaScript code related to Firebase integration: import firebase from 'firebase/app' import 'firebase/firestore' const config = { ap ...

Assistance is required to navigate my character within the canvas

Having trouble getting the image to move in the canvas. Have tried multiple methods with no success. Please assist! var canvas = document.getElementById("mainCanvas"); canvas.width = document.body.clientWidth; canvas.height = document.body.clientHeight; ...

Adding query parameters to the end of every link on a webpage

Wondering how to automatically add GET values to the end of each anchor href on a webpage? For instance, if you input google.com?label=12&id=12 I want to ensure that "?label=12&id=12" gets added to the end of every single href in an anchor tag on ...

Updating the Navigation Bar and Theme in CRM Dynamics 2013 to Reflect Your Organization's Branding

In my CRM Dynamics 2013 setup, I am faced with a unique challenge. I need to customize the Organization navigation bar based on which organization is currently loaded. Specifically, I have two organizations - QA and PROD. When a user switches to the QA org ...

Access an attribute using slashes in Jquery

I've been struggling to use jQuery to select an object based on a unique filename attribute. However, I'm facing issues with escaping slashes when the selector is created using a variable. Despite spending hours trying different combinations, I s ...

Invoking a JavaScript function within a different JavaScript function

Is there a way to ensure that the JavaScript function works properly even when using a text editor? var editor = $('#CKEditor1').ckeditorGet(); editor.on("instanceReady", function () { this.document.on("keydown", function (event) { ...

Restrict Vue Router access to specific pages only

Once a user has logged in for the first time, they are directed to the CreateProfile page where they can enter their profile information. I want to restrict access to this page so that if the user manually types the URL into their browser (e.g. www.myproje ...

The Puppeteer software does not automatically shut down the browser once the task is complete

Currently, I have set up puppeteer on my Ubuntu server with Express and Node.js like so: var puppeteer = require('puppeteer'); var express = require('express'); var router = express.Router(); /* GET home page. */ router.get('/&ap ...