`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

Encountering a Uncaught TypeError when attempting to split an undefined property, but issue is limited to certain pages

Recently, I've encountered an issue with iziModal on specific pages where I'm getting an error message. The error I'm facing is: Uncaught TypeError: Cannot read property 'split' of undefined at r.fn.init.t.fn.(anonymous fu ...

Real-time updates using Express.js and Redis

Looking for guidance on managing real-time changes from Redis in Express.js. I am in need of fresh data from Redis every time there is an update. Can someone provide a helpful example or solution for this? ...

What is the best way to transfer a base64 image string obtained from pouchdb to a different api as a file?

I have stored an image in base64 format within pouchdb, and I am now looking to upload the same file to a specific API on the server. Is there a way for me to extract the file path or convert the base64 image back into a regular file so that it can be acc ...

Could you please provide me with the option to send a list of the

Is there a way to send output via email instead of displaying it in the following div: <div id="fullCalendar" ></div> After spending a whole night searching online, I couldn't find a solution. As I'm not very familiar with jQuery pr ...

Mirror the input text to another input within a for loop

I have a list of questions displayed, each with an input field for entering a phone number. How can I use jQuery or JavaScript in a for-loop to mirror the text entered in the first phone input box to all subsequent phone input boxes? ..Enter your phone... ...

Angular.js guarantees fetching data with $http.get

I encountered an issue with my service that makes an $HTTP.GET request. Initially, I tried to access the object $$state but found that the value inside was undefined. To resolve this, I delved into learning about promises and asynchronous calls by reading ...

Exclude the use of ':any' in React component

Currently, I am new to React and facing a challenge in sending a variable and function to my component. I understand that using :any is not the best practice, so I am seeking a better solution. I am working on creating a modal and passing data to my compo ...

Smooth Div Scroll experiencing display issues

Trying to integrate the smooth div scroll, specifically the "Clickable Logo Parade" found at: Successfully implemented it on a blank page exactly as desired, but encountering issues when inserting it into the current layout. Could something be causing int ...

Unable to redirect to a sub state and display the named view using UI Router

Attempting to implement a named view for a sub state and navigate to it using $state.go from its parent state according to the instructions provided in the document, but encountering issues with success. Access the editable Plunker here. <!DOCTYPE ...

Preventing ReactJS tooltips from exceeding the boundaries of the screen

Here is a simple demo showcasing blocks with tooltips that appear when hovered over. However, there seems to be an issue with the functionality. The tooltip should ideally be displayed either from the left or right side of the block. To determine the size ...

"Troubleshooting the issue of nested jQuery Ajax requests failing to execute properly

UPDATE: I'm getting an error saying that my li tag is considered an illegal token. I'm unsure how to resolve this issue as I believed I was appending it within a ul tag I'm tasked with fetching a JSON array of public Github repositories and ...

The disappearance of the "Event" Twitter Widget in the HTML inspector occurs when customized styles are applied

Currently, I am customizing the default Twitter widget that can be embedded on a website. While successfully injecting styles and making it work perfectly, I recently discovered that after injecting my styles, clicking on a Tweet no longer opens it in a ne ...

Preventing scrolling in one div while focusing on another div

I am currently in the process of creating a website that functions as a single page site. The main feature of the site is a masonry grid of images. When a user clicks on an item, a detailed panel slides in from the left. Once the panel is closed, it slide ...

Despite importing jQuery, the variable '$' cannot be located

Whenever I try to click the button labeled test, it doesn't do anything. However, an error message appears in the console debug indicating: Error: Unable to locate variable '$'. I suspect this might be a jQuery issue, even though I' ...

What is the best way to extract the information from the checkbox in a MUI datatable?

I am struggling to transfer an array with checked values to another table in order to display only those values. How can I achieve this? I am relatively new to using react and I find it challenging to grasp how some of the functions and components work. I ...

`Storing modified PDF containing interactive form elements using integrated Chrome viewer and transferring it to a remote server`

Our current setup utilizes the default embedded Chrome PDF viewer to showcase a PDF document with interactive form fields, enabling users to conveniently fill out the form. Once completed, they can click on the download button to save the form with or with ...

JavaScript - Employing the .every function with an array containing objects

Is it possible to use the array.every method on multidimensional arrays? The structure of my array is as follows: tabs=[ {label: string, icon: icon, routerLink: link}, {label: string, icon: icon, routerLink: link}, {label: string, icon: icon, routerLink: ...

Capturing screen captures while using Protractor on BrowserStack

Greetings! I am currently in the process of capturing screenshots using protractor and browserstack, and I have the following conf.js file: var HtmlReporter = require('protractor-html-screenshot-reporter'); var reporter=new HtmlReporter({ b ...

Error encountered in react-test-renderer test due to TypeError: Attempting to access properties of an undefined variable, specifically trying to read the 'current

Having trouble with my unit tests using react-test-renderer. Even though the component I'm testing is working fine, the test won't pass. Can someone please help me figure out what I'm doing wrong? Below is the code for the component: // ...

Looking to display a different HTML document in a div - any suggestions?

I am in the process of creating a website that will feature four tiles on the front page. Once a tile is clicked, I would like the content to appear in a window that has a slight transparency to allow the main page to show through. So far, I have managed ...