In order to implement the functionality in JavaScript, the .slider::-webkit-slider

I need to create a slider that adjusts the size of the dot based on the value input. For example, when the value is 1, the dot size should be 10px and for a value of 100, it should be 100px.

Is there a way to dynamically change the height of .slider::-webkit-slider-thumb using JavaScript?

var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value;

slider.oninput = function() {
  output.innerHTML = this.value;
}
.slidecontainer {
  width: 100%;
}

.slider {
  -webkit-appearance: none;
  width: 100%;
  height: 15px;
  border-radius: 5px;
  background: #d3d3d3;
  outline: none;
  opacity: 0.7;
  -webkit-transition: .2s;
  transition: opacity .2s;
}

.slider:hover {
  opacity: 1;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: #4CAF50;
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: #4CAF50;
  cursor: pointer;
}
<h1>Round Range Slider</h1>

<div class="" "slidecontainer">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
  <p>Value: <span id="demo"></span></p>
</div>

My attempt so far:

document.getElementById("myRange").style.webkitTransform = "height: 100px";

Answer №1

Before delving into the question, it's important to note that ::-webkit-slider-thumb is a pseudo element, not part of the DOM. Manipulating these elements with JavaScript can be cumbersome.

A more effective solution would involve replacing the thumb entirely with another element.


To address the question at hand: you can inject styles into a <style> element with a specified attribute when the slider value changes.

Add the following code snippet to your header:

<style data="test" type="text/css"></style>

Then target the element and apply CSS like so:

var style = document.querySelector('[data="test"]');
style.innerHTML = ".class { property: value; }";

This method, alongside your existing code available in this link, can solve your issue.


Demo:

var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
var style = document.querySelector('[data="test"]');

setData(slider.value);

slider.oninput = function() {
    setData(this.value);
}

function setData(x){
    output.innerHTML = x;
    style.innerHTML = ".slider::-webkit-slider-thumb { width: " + x + "px !important; height: " + x + "px !important; }";
}
.slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 25px;
    height: 25px;
    border-radius: 50%;
    background: #4CAF50;
    cursor: pointer;
}
.slider {
    margin-top: 40px;
    -webkit-appearance: none;
    width: 100%;
    height: 15px;
    border-radius: 5px;
    background: #d3d3d3;
    outline: none;
}
<style data="test" type="text/css"></style>

<input type="range" min="1" max="100" value="50" class="slider" id="myRange">
<p>Value: <span id="demo"></span></p>

You mentioned:

So value 1 will be a size of 10px and value 100 will be size 100px.

If you prefer an approach where values 1 to 10 represent 10px, whereas anything above 10 reflects the actual pixel value, modify the line:

style.innerHTML = "...";

to

style.innerHTML = ".slider::-webkit-slider-thumb { width: " + (x < 10 ? 10 : x)  + "px !important; height: " + (x < 10 ? 10 : x) + "px !important; }";

An interactive demo showcasing this version is accessible HERE

Answer №2

Discover the Magic of CSS Variables in 2023

The existing solution may get the job done, but it's quite lengthy and can be challenging to grasp. Luckily, there's a simpler way to achieve the same result...

All you need to do in your CSS is define a variable like --sliderSize:

.slider {
  --sliderSize: 25px; /* <---- Include this line */
  -webkit-appearance: none;
  width: 100%;
  height: 15px;
  border-radius: 5px;
  background: #d3d3d3;
  outline: none;
  opacity: 0.7;
  -webkit-transition: .2s;
  transition: opacity .2s;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: var(--sliderSize); /* <---- Utilize the variable here */
  height: var(--sliderSize); /* <--- and here */
  border-radius: 50%;
  background: #4CAF50;
  cursor: pointer;
}

Then, in your JavaScript code, simply assign a value to the variable using setProperty:

const newSliderSize = '100px'; // This could come from user input or elsewhere
document.getElementById("myRange").style.setProperty('--sliderSize', newSliderSize);

Check out this interactive example showcasing the concept:

    const newSliderSize = '50px'; // Dynamic value assignment example
    document.getElementById("myRange").style.setProperty('--sliderSize', newSliderSize);
  .slider {
      --sliderSize: 25px; /* <---- Include this line */
      -webkit-appearance: none;
      width: 100%;
      height: 15px;
      border-radius: 5px;
      background: #d3d3d3;
      outline: none;
      opacity: 0.7;
      -webkit-transition: .2s;
      transition: opacity .2s;
    }

    .slider::-webkit-slider-thumb {
      -webkit-appearance: none;
      appearance: none;
      width: var(--sliderSize); /* <---- Utilize the variable here */
      height: var(--sliderSize); /* <--- and here */
      border-radius: 50%;
      background: #4CAF50;
      cursor: pointer;
    }
<input type="range" min="1" max="100" id="myRange" class="slider" />

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

Enhance your website with a dynamic jQuery gallery featuring stunning zoom-in

I am currently working on a mobile website project and I'm in need of a gallery feature that allows users to zoom in on images and swipe through them using touch gestures. After some research, I haven't been able to find a suitable solution in j ...

Prevent repetitive content on your Node.js server

After realizing my small image hosting has many duplicate content, I am looking for a solution to prevent this issue in the future. My idea is to use either checksum or hash code so that whenever a new file is uploaded, it will be hashed and compared with ...

The OrbitControls function is not able to be instantiated as a constructor

I've been working on creating a WebVR environment for the past week using Three.js, but I'm having trouble getting the VR controls to function correctly. Here are some of the things I've tried: 1. Adding packages in my node_modules and imp ...

The filter designed for rearranging elements will not function

let wrap = document.querySelector(".wrap"); let temp = document.querySelector(".temp"); temp.addEventListener("click", (event) => { // grabs the first paragraph child element, which is working correctly console.log(wrap.children[0].firstElementChild ...

What is the best way to combine cells within a single column?

Exploring the code snippet utilizing ag-grid with Vue 3. <script setup lang="ts"> import "ag-grid-community/dist/styles/ag-grid.css"; import "ag-grid-community/dist/styles/ag-theme-alpine.css"; import { AgGridVue } from ...

Can the JS DOM Element method be utilized in Postman to navigate through an HTML document?

How can I declare this in the Postman sandbox? I am looking for a solution like the following: var h1Text = window.document.querySelector("h1").innerHTML; console.log(h1Text); Additionally, I need to search for something in the following manner: I have ...

Ways to display dynamic content within a div using Bootstrap's vertical tab through a click event in PHP

I have been working on displaying static content in a div using Bootstrap vertical tabs, similar to this: Link: However, I am facing an issue with showing dynamic content in a div using a Bootstrap vertical tab through a PHP click event. I have been try ...

Why am I encountering this export issue while attempting to integrate a NextAuth.js Provider with Next.js?

Embarking on my first project in React/Next.js, I opted to employ NextAuth.js for authentication. Following the initial steps outlined in the Getting Started guide provided by NextAuth, I have successfully set up a [...nextauth].js page featuring the code ...

Streamlined Infinite Scrolling Data Visualization Using SVG Implemented in HTML and CSS

I have developed a dynamic graph that continuously updates with new data points being plotted. Right now, I am utilizing requestAnimationFrame() to render the element positions 30 times per second, but performance can be sluggish with numerous SVG element ...

Using CSS grid to wrap three divs simultaneously

Can CSS grid be utilized to style three divs so that they wrap simultaneously when the screen size decreases? This transition: +---------+--+---------+--+---------+ | div | | div | | div | +---------+--+---------+--+---------+ to this: +- ...

Is there a way for me to retrieve the input text value and display it in my modal window?

When using jQuery, I want my text field to appear in my modal. The code seems to work fine when I check the checkbox, but nothing shows up when I try to use the text field. How can I resolve this issue? Body <body> <div id="hasildetail" clas ...

Aligning object in middle of a responsive image using Bootstrap

I'm having trouble centering a button on an image using responsive design techniques. I am currently utilizing Bootstrap 3 and have attempted to use CSS margin properties to center the button, but this method is not ideal especially on smaller screens ...

Submit numerous queries to verify the presence of a name and assign it to the name attribute

I have a collection of unique devices. As part of a process, I need to assign a default name to each device ("DeviceX" - where X is a sequential number), but some of the names may already be in use. To handle this, I must make a request to check ...

Strange spacing issues with inline-block elements and struggling to vertically center content within them

I feel like I'm losing my mind trying to figure this out... any help would be greatly appreciated. There are three divs on the page that need to sit on a single line. They must be square with rounded corners, so setting width and height is necessary ...

How can I target the first checkbox within a table row using jQuery?

I am seeking a way to determine if the first checkbox in a table row is selected, rather than checking all checkboxes within that particular row. Currently, I am using this code: var b = false; $j('#tb1 td input:checkbox').each(function(){ ...

Looping through a JSON object to create a table showcasing public holidays

Currently, I am working on creating a table that lists all the public holidays. The table comprises rows with holiday names on the left and dates on the right. Unfortunately, the table only displays data from the final array of the JSON object, whereas I ...

Retrieve the values of a function using the Firebase database

Hey, I'm in a bit of a pickle trying to retrieve the values returned by my function. I can see them just fine in the console log, but how do I actually access them when calling the function? function getprofile(useruid) { return firebase.database ...

Resize background image to perfectly fit within div dimensions

One of my divs has a background image that is getting clipped: <div style='text-align:center;background-image: url(/media/img_1_bg.jpg);background-repeat:no-repeat;width:450px;height:900px;' id="mainpage" align="center"> Is there a soluti ...

Is it necessary to overlook Java Script when conducting load testing on my website?

We are in the process of developing a robust web application that needs to be able to handle a significant amount of traffic. I have been conducting load tests on a HP (Proliant DL 380) server with two 3.6GHz Xeon CPUs and 16GB of RAM. To perform these tes ...

Ensure there is a sufficient gap between the top and bottom icons within the Material-UI Drawer

I'm having difficulty articulating this, but I'd like to add two different sets of icons to the Drawer component. Set 1 should be displayed at the top in a standard column format, similar to the examples provided by them. Set 2 should go at the b ...