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
This article shares the specific code of Vue usin...
Typical layout examples As shown in the above pic...
I have been researching Linux recently and tried ...
Table of contents Background Configuring DHCP Edi...
Copy code The code is as follows: jQuery.cookie =...
As the number of visits increases, for some time-...
Preface Js is the most commonly used code manipul...
This article example shares the specific code of ...
Since I often install the system, I have to reins...
This article example shares the specific code of ...
Table of contents 1. Basic usage and logic 2. Fea...
first step Delete it once with the built-in packa...
What is MIME TYPE? 1. First, we need to understand...
1. The startup menu is to move the cursor to the ...
Introduction: The disadvantages of storing all da...