Ways to create a function to adjust the color of a slider track

I have implemented a custom CSS range slider that changes the color of the track as I move through it. The initial color is grey, but it changes as I slide. This functionality works perfectly in FireFox and IE.

However, I am facing an issue with Chrome. How can I resolve this?

Here is the HTML code:

   <text-size-slider min="12" max="36" unit="px" value="18" step="0">
        <!-- Custom AngularJS Directive Ends Here -->
    </text-size-slider>

And here is the JavaScript code:

angular.module('textSizeSlider', []) .directive('textSizeSlider', ['$document', function ($document) {

  var ctrl = ['$scope', '$element', function ($scope, $element) {
      $scope.position = 0;
      $scope.updatepointer = () => {
          var input = $element.find("input");
          var width = input[0].offsetWidth - 16; // 16 for offsetting padding
          var ratio = ($scope.textSize - $scope.min) / ($scope.max - $scope.min);
          var position = width * ratio;
          $scope.position = Math.trunc(position);

      }

  }]

  return {
      controller: ctrl,
      restrict: 'E',
      template: '<div class="text-size-slider"><span class="pointer" style="left:{{position}}px;"><span>{{textSize}}</span></span><span class="small-letter" ng-style="{ fontSize: min + unit }"><small>T</small>T</span> <input type="range" min="{{ min }}" max="{{ max }}" step="{{ step || 0 }}" ng-model="textSize" class="slider" value="{{ value }}" ng-change="updatepointer()" /> <span class="big-letter" ng-style="{ fontSize: max + unit }"><small>T</small>T</span></div>',
      scope: {
          min: '@',
          max: '@',
          unit: '@',
          value: '@',
          step: '@'
      },
      link: function (scope, element, attr) {
          scope.textSize = scope.value;

          scope.$watch('textSize', function (size) {
              $document[0].body.style.fontSize = size + scope.unit;
              scope.updatepointer();
          });
      }
  }

  }]);

You can view the Plunker link here.

Expected output:

https://i.sstatic.net/qfCdm.png

Answer №1

I stumbled upon the solution below

Take a look at this reference

http://stackoverflow.com/questions/1679577/get-document-stylesheets-by-name-instead-of-index

According to the topic mentioned, it seems that we are unable to modify the input's shadow-root using JavaScript, but there is a way to change the styleSheet element. I borrowed the setStyleRule function from the aforementioned discussion and applied it to alter the CSS of the element.

LATEST UPDATE

Introducing a grey layer for tracking purposes.

// Your code snippets go here

// It's your AngularJS directive code, where you define functionality related to text size slider
...
/* Custom Styles for Text Slider Directive */

body {
  color: red;
  font-family: "HelveticaNeue", "Helvetica Neue", "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-weight: normal;
  margin: 0;
}

header {  
  background-color: white;  
  padding: 60px 40px;
}

h1 {
  font-size: 200%;
}
... more styles continue ...
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

    <header ng-app="textSizeSlider">
        <!-- Your custom AngularJS Directive goes here -->
        <text-size-slider min="12" max="36" unit="px" value="18" step="0">
            <!-- END of your custom AngularJS Directive -->
        </text-size-slider>
    </header>

    <p>
        <strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry.
    </p>

Answer №2

I would highly recommend testing out this code on Chrome to see if it functions as expected.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>

    input[type='range'] {
      width: 200px;
      height: 3px;
      -webkit-appearance: none;
      background-color: #1682fb;
      display: flex;
      outline: none;
        border: none;
    } 

    input[type='range']::-webkit-slider-runnable-track {
      height: 5px;
      -webkit-appearance: none;
      margin-top: -10px;
    }

    input[type='range']::-webkit-slider-thumb {
      width: 15px;
      -webkit-appearance: none;
      height: 15px;
      border-radius: 50%;
      cursor: pointer;
      background: #1682fb; 
      box-shadow: /* Multiple values for different positions */ ;
    }
    </style>
</head>
<body>
    <div style="overflow: hidden; height: 15px;padding-top: 10px;padding-right:3px;width: 200px;">
    <input type="range" name="" id="" min="=0" max="100" step="1">
</div>
</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

Why is the inline-block element in the list displaying on two lines? The text contains a number, but does it affect the layout?

What could be the reason for 'Maroon 5' moving down to a second line? Despite adding extra characters and testing with an integer, the issue persists. <ul id="soundsLike"> <li>Foo Fighters</li> <li>Maroon 5</ ...

Typescript: Ways to fix the error 'rxjs/Rx does not have any exported member 'SubscriptionLike'

I'm attempting to replicate the steps outlined in this tutorial found here https://www.youtube.com/watch?v=gxCu5TEmxXE. However, upon running tsc -p, I encounter an error. Is there a specific import that I am missing? ERROR: node_modules/@angular/co ...

Is it possible to modify the local reference point of an object in three.js?

Is there a method to readjust the center point of an STL file once it has been relocated or turned? For example, if the original rotation of my object is set at (0,0,0), and then I rotate it to (20,70,90) degrees, is it possible to modify the local origi ...

Can object methods be called using inline event attributes in an HTML tag?

Exploring the depths of core JavaScript has been my latest obsession. It's fascinating how we can easily trigger functions within the global context using events like this. HTML <span class="name" onclick="clicked()">Name</span> JavaSc ...

Using JQuery and JavaScript to store and dynamically apply functions

I have a code snippet that looks like this:. var nextSibling = $(this.parentNode).next(); I am interested in dynamically changing the next() function to prev(), based on a keypress event. (The context here is an input element within a table). Can someo ...

retrieve the class name of a reusable component in HTML

I have been assigned a web scraping project and I am required to scrape data from the following website for testing: The HTML: <div class="quote" itemscope itemtype="http://schema.org/CreativeWork"> <span class="text& ...

Using React.js to dynamically change a CSS class depending on a certain condition

I am trying to assign a CSS class to a div based on a specific condition. The issue I am facing is that it always takes the last condition. Below is the code snippet: When I run the code, I receive logos.length as 3 <div className= "${ (this.state. ...

Is `send()` considered an anonymous function?

I am testing ajax on a local server, but encountering an issue where the console is showing that send() is being identified as an anonymous function and I am receiving a blank document. The console displays: XHR finished loading: GET "http://localhost/js ...

Closing webdriver instance in Selenium within a loop

Greetings fellow Selenium enthusiasts! I am fairly new to using Selenium and I am encountering a puzzling issue that I can't seem to crack. Within my web scraping script, I have a for loop that is functioning properly. However, during the execution, ...

Difficulty replicating 3 input fields using JavaScript

Combining the values of 3 input fields into 1 Displaying 'fname mnane lname' in the fullname input field. Then copying the fullname value into another input field called fullname2. Check out the code below for both HTML and JavaScript implemen ...

Issue with Nuxt JS Vuex data not updating in real-time upon page load

Within my Nuxt JS 2.9 application, I have implemented Vuex to store and retrieve data in my layout. The issue I am facing is that when I set data into the Vuex store on a page-specific basis, it requires me to refresh the page in order to see the updated d ...

Determine the value from an object in the view by evaluating a string in dot notation

Seeking assistance with a recurring issue I've encountered lately. Imagine having two objects in AngularJS: $scope.fields = ['info.name', 'info.category', 'rate.health'] $scope.rows = [{ info: { name: "Apple", cate ...

What steps can I take to improve the design of this layout created using Twitter Bootstrap?

Currently, I am designing a sample form layout using Twitter Bootstrap. The form displays fields horizontally, with a link between each field. My goal is to have bullet points appear on the right side of the fields when the link is clicked. However, the al ...

Arrange three images and three text lists in a horizontal layout with responsiveness

Struggling with a HTML/CSS challenge here! I'm attempting to vertically align 3 images and text in the pattern of image, text, image, text, image, text. Everything looks great on my 13" MacBook, but once I start resizing the window, chaos ensues. Any ...

Execute an AJAX call to remove a comment

Having some trouble deleting a MySQL record using JavaScript. Here is the JavaScript function I am trying to use: function deletePost(id){ if(confirm('Are you sure?')){ $('#comment_'+id).hide(); http.open("get","/i ...

The submission of the form is prevented upon updating the inner HTML

I am in the process of creating a website that will incorporate search, add, update, and delete functionalities all on a single webpage without any modals. The main focus of this webpage is facility maintenance. Adding facilities works smoothly; however, i ...

Troubleshooting Unforeseen Results with Bootstrap 5 Justify Content Center and Container Fluid

In my attempt to design a div that occupies 8 columns, centered within a wide viewport, and expands to take up all 12 columns in any other viewport sizes, I encountered an issue. Although I used justify-content-center to align the content, it seems that o ...

The resolveMX function in Google Cloud Functions is encountering issues when trying to process a list of domains

Here is the task at hand. I have a large list of domains, over 100,000 in total, and I need to iterate through them using a foreach loop to resolve MX records for each domain. Once resolved, I then save the MX records into another database. Below is the c ...

Saving persistent values in AngularJSIn AngularJS, you can

Can values or constants be stored in a recipe that includes all local storage values with assigned names, such as: username:$localstorage.get('M_NAME') mem_id:$localstorage.get('MEMBER_ID') position:$localstorage.get('M_POSITION&a ...

Javascript added link padding not functioning correctly in Internet Explorer

After attempting to find a solution on my own, I've come to realize that this situation is quite unusual. The issue at hand involves adding a class to a link using JavaScript (specifically jQuery, although the framework may not be relevant in this ca ...