JS implements dragging the progress bar to change the transparency of elements

JS implements dragging the progress bar to change the transparency of elements

What I want to share today is to use native JS to drag the progress bar to change the transparency of the element. The effect is as follows:

The following is the code implementation, you are welcome to copy and paste.

<!DOCTYPE html>
<html>
 
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Native JS drag progress bar to change element transparency</title>
    <style>
        #parent {
            width: 400px;
            height: 20px;
            background: #CCC;
            position: relative;
            margin: 20px auto;
        }
 
        #div1 {
            width: 20px;
            height: 20px;
            background: red;
            cursor: pointer;
            position: absolute;
        }
 
        #div2 {
            width: 300px;
            height: 300px;
            margin: 0 auto;
            filter:alpha(opacity:0);
            opacity: 0;
            background: yellow;
        }
    </style>
 
    <script>
        window.onload = function () {
 
            var oDiv = document.getElementById('div1');
            var oParent = document.getElementById('parent');
            var oDiv2 = document.getElementById('div2');
 
            oDiv.onmousedown = function (ev) {
 
                var oEvent = ev || event;
                //Calculate the mouse position relative to the slider var disX = oEvent.clientX - oDiv.offsetLeft;
 
                document.onmousemove = function (ev) {
 
                    var oEvent = ev || event;
                    //Calculate the dynamic left value of the slider var l = oEvent.clientX - disX;
 
                    //Limit the drag range if (l < 0) {
 
                        l = 0;
 
                    } else if (l > oParent.offsetWidth - oDiv.offsetWidth) {
 
                        l = oParent.offsetWidth - oDiv.offsetWidth;
                    }
 
                    oDiv.style.left = l + 'px';
 
                    //Calculate the ratio of the drag moving distance to the total draggable range var scale = l / (oParent.offsetWidth - oDiv.offsetWidth);
 
                    //Let Div2 gradually show and hide as the mouse is dragged oDiv2.style.filter = 'alpha(opacity:' + 100 * scale + ')';
                    oDiv2.style.opacity = scale;
 
                };
 
                document.onmouseup = function () {
 
                    document.onmousemove = null;
                    document.onmouseup = null;
                };
            };
        };
    </script>
</head>
 
<body>
    <div id="parent">
        <div id="div1"></div>
    </div>
    <div id="div2"></div>
</body>
 
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JavaScript implements draggable progress bar
  • JavaScript implements horizontal progress bar drag effect
  • js to achieve sliding progress bar effect
  • JS implements circular progress bar drag and slide

<<:  Will the deprecated Docker be replaced by Podman?

>>:  HTML table tag tutorial (46): table footer tag

Recommend

Vue uses element-ui to implement menu navigation

This article shares the specific code of Vue usin...

How to perfectly implement the grid layout with intervals on the page

Typical layout examples As shown in the above pic...

Summary of common Linux distribution mirror source configuration

I have been researching Linux recently and tried ...

How to automatically deploy Linux system using PXE

Table of contents Background Configuring DHCP Edi...

jQuery manipulates cookies

Copy code The code is as follows: jQuery.cookie =...

Mysql master/slave database synchronization configuration and common errors

As the number of visits increases, for some time-...

Detailed explanation of the role of the new operator in Js

Preface Js is the most commonly used code manipul...

js to realize login and registration functions

This article example shares the specific code of ...

MySQL5.7.21 decompressed version installation detailed tutorial diagram

Since I often install the system, I have to reins...

Vue implements login jump

This article example shares the specific code of ...

Detailed explanation of JavaScript error capture

Table of contents 1. Basic usage and logic 2. Fea...

Linux completely removes node.js and reinstalls it through the yum command

first step Delete it once with the built-in packa...

What is MIME TYPE? MIME-Types type collection

What is MIME TYPE? 1. First, we need to understand...

How to reset your Linux password if lost

1. The startup menu is to move the cursor to the ...

MySQL foreign key (FOREIGN KEY) usage case detailed explanation

Introduction: The disadvantages of storing all da...