How To Create Custom Range Sliders Using CSS And JavaScript

admin_img Posted By Bajarangi soft , Posted On 06-10-2020

In Java Script we can create dynamic rang slider to to display the current value so today we discuss how to create range slider using java script

How To Create Custom Range Sliders Using CSS And JavaScript

Creating a Range Slider
Step 1
: Create index.html and implement below code.

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

Step 2: Now implement CSS code for custom range slider.
.slidecontainer {
    width: 100%; /* Width of the outside container */
}

/* The slider itself */
.slider {
    -webkit-appearance: none;  /* Override default CSS styles */
    appearance: none;
    width: 100%; /* Full-width */
    height: 25px; /* Specified height */
    background: #d3d3d3; /* Grey background */
    outline: none; /* Remove outline */
    opacity: 0.7; /* Set transparency (for mouse-over effects on hover) */
    -webkit-transition: .2s; /* 0.2 seconds transition on hover */
    transition: opacity .2s;
}

/* Mouse-over effects */
.slider:hover {
    opacity: 1; /* Fully shown on mouse-over */
}

/* The slider handle (use -webkit- (Chrome, Opera, Safari, Edge) and -moz- (Firefox) to override default look) */
.slider::-webkit-slider-thumb {
    -webkit-appearance: none; /* Override default look */
    appearance: none;
    width: 25px; /* Set a specific slider handle width */
    height: 25px; /* Slider handle height */
    background: tomato; /* Green background */
    cursor: pointer; /* Cursor on hover */
}

.slider::-moz-range-thumb {
    width: 25px; /* Set a specific slider handle width */
    height: 25px; /* Slider handle height */
    background: tomato; /* Green background */
    cursor: pointer; /* Cursor on hover */
}
 

Step 3: Now Implement java script to Create a dynamic range slider to display the current value, with JavaScript:.

<script>
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value; // Display the default slider value

// Update the current slider value (each time you drag the slider handle)
slider.oninput = function() {
    output.innerHTML = this.value;
}
</script>


Complete Code For Creating Custom Range Sliders Using CSS And JavaScript

<!DOCTYPE html>
<html>
<head>
    <title>How To Create Progress Bar Using CSS And JavaScript</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<style>
    .slidecontainer {
        width: 100%;
    }

    .slider {
        -webkit-appearance: none;
        width: 100%;
        height: 25px;
        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;
        background: tomato;
        cursor: pointer;
    }

    .slider::-moz-range-thumb {
        width: 25px;
        height: 25px;
        background: tomato;
        cursor: pointer;
    }
</style>
<body>
<div class="container">
    <br>
    <div class="text-center">
        <h1 style="color: tomato">How To Create Custom Range Sliders Using CSS And JavaScript</h1>
    </div>
    <div class="well">
        <h1>Custom Range Slider</h1>
        <p>Drag the slider to display the current value.</p>

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

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

            slider.oninput = function () {
                output.innerHTML = this.value;
            }
        </script>

    </div>
</div>
</body>
</html>

 

Related Post