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:
|
<<: Will the deprecated Docker be replaced by Podman?
>>: HTML table tag tutorial (46): table footer tag
Preface: I used the official nginx proxy_cache as...
This article example shares the specific code of ...
This article uses examples to explain the concept...
Sometimes we need to control whether HTML elements...
Preface Execute the show create table <tablena...
The results are different in Windows and Linux en...
Table of contents 1. Introduction to Slow Log 2. ...
This article records the installation tutorial of...
1. Check and install pssh, yum list pssh 2. Becau...
This article example shares the specific code of ...
introduce If you are using an OSS storage service...
1 / Copy the web project files directly to the we...
In the Docker system learning tutorial, we learne...
There are some problems with the compressed versi...
background There is a Tencent Linux cloud host, o...