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

nginx proxy_cache batch cache clearing script introduction

Preface: I used the official nginx proxy_cache as...

JavaScript to achieve digital clock effect

This article example shares the specific code of ...

Detailed explanation of the concept, principle and usage of MySQL triggers

This article uses examples to explain the concept...

Display and hide HTML elements through display or visibility

Sometimes we need to control whether HTML elements...

What is COLLATE in MYSQL?

Preface Execute the show create table <tablena...

How to manually deploy war packages through tomcat9 on windows and linux

The results are different in Windows and Linux en...

Summary of MySQL slow log related knowledge

Table of contents 1. Introduction to Slow Log 2. ...

CentOS 6.5 configuration ssh key-free login to execute pssh command explanation

1. Check and install pssh, yum list pssh 2. Becau...

Vue implements simple notepad function

This article example shares the specific code of ...

Several common ways to deploy Tomcat projects [tested]

1 / Copy the web project files directly to the we...

The difference between ENTRYPOINT and CMD in Dockerfile

In the Docker system learning tutorial, we learne...

MySQL compressed package version zip installation configuration method

There are some problems with the compressed versi...

How to use Nginx to realize the coexistence of multiple containers in the server

background There is a Tencent Linux cloud host, o...