[前端] HTML+CSS+JS模仿win10亮度调节效果的示例代码

1616 0
Honkers 2022-10-19 16:08:43 | 显示全部楼层 |阅读模式
HTML+CSS+JS模仿win10亮度调节效果
代码
  1. <!doctype html>
  2. <html>
  3.         <head>
  4.                 <meta charset="utf-8">
  5.                 <title>模仿win10的亮度调节</title>
  6.                 <style>
  7.                         .control_bar{
  8.                                 height:200px;
  9.                                 width:500px;
  10.                                 border-bottom:3px solid #888888;
  11.                         }
  12.                         .control_bar_cursor{
  13.                                 height:25px;
  14.                                 width:8px;
  15.                                 background: #505151;
  16.                                 border-radius:5px;
  17.                                 margin-top:-12.5px;
  18.                                 position:relative;
  19.                                 top:0;
  20.                                 left:0;
  21.                         }
  22.                         .control_bar_cursor:hover{
  23.                                 background:white;
  24.                         }
  25.                         #control_bar_mask{
  26.                                 margin-top:-203px;
  27.                                 width:0px;
  28.                         }
  29.                         .mask{
  30.                                 position:fixed;
  31.                                 bottom:0;
  32.                                 top:0;
  33.                                 left:0;
  34.                                 right:0;
  35.                                 background:black;
  36.                                 z-index:-1;
  37.                         }
  38.                 </style>
  39.         </head>
  40.         <body>
  41.                 <div class="mask"></div>
  42.                 <div class="control_bar"></div>
  43.                 <div class="control_bar" style="border-bottom:3px solid #505151;" id="control_bar_mask"></div>
  44.                 <div class="control_bar_cursor"></div>
  45.         </body>
  46.         <script>
  47.                 window.onload = function(){
  48.                         var control_bar = document.getElementsByClassName("control_bar")[0];
  49.                         var control_bar_mask = document.getElementById("control_bar_mask");
  50.                         var control_bar_cursor = document.getElementsByClassName("control_bar_cursor")[0];
  51.                         var def_left = control_bar_cursor.offsetLeft;
  52.                         var mask = document.getElementsByClassName("mask")[0];
  53.                         document.body.onmousedown = function(){
  54.                                 window.onmousemove = function(){
  55.                                         var cursor_X = event.clientX;
  56.                                         var cursor_Y = event.clientY;
  57.                                         if(cursor_X < def_left){
  58.                                                 control_bar_cursor.style.left = 0;
  59.                                         }else if(cursor_X > control_bar.offsetWidth + def_left){
  60.                                                 control_bar_cursor.style.left = control_bar.offsetWidth;
  61.                                         }else{
  62.                                                 control_bar_cursor.style.left = cursor_X - def_left + "px";
  63.                                         }
  64.                                         //亮度比
  65.                                         var proportion = parseInt(control_bar_cursor.offsetLeft - def_left) / parseInt(control_bar.offsetWidth - 1);
  66.                                         control_bar_mask.style.width = proportion * control_bar.offsetWidth + "px";
  67.                                         mask.style.opacity = 1 - proportion;
  68.                                         };
  69.                                 window.onmouseup = function(){
  70.                                         window.onmousemove = null;
  71.                                 };
  72.                         };
  73.                 };
  74.         </script>
  75. </html>
复制代码
1.将各个元素的样子写出来
这里为了方便好观察给body添加了一个背景颜色
html
  1. <div class="control_bar">
  2. </div>
  3. <div class="control_bar" style="border-bottom:3px solid #505151;"  
  4. id="control_bar_mask>
  5. </div>
  6. <div class="control_bar_cursor">
  7. </div>
复制代码
css
  1. body{
  2.     background:back;
  3. }
  4. .control_bar{
  5.     height:200px;
  6.     width:500px;
  7.     border-bottom:3px solid #888888;
  8. }
  9. .control_bar_cursor{
  10.     height:25px;
  11.     width:8px;
  12.     background: #505151;
  13.     border-radius:5px;
  14. }
复制代码
效果图


2. 将各个元素叠到一起
css
  1. body{
  2.     background:black;
  3. }
  4. .control_bar{
  5.     height:200px;
  6.     width:500px;
  7.     border-bottom:3px solid #888888;
  8. }
  9. .control_bar_cursor{
  10.     height:25px;
  11.     width:8px;
  12.     background: #505151;
  13.     border-radius:5px;
  14.     margin-top:-12.5px;
  15.     position:relative;
  16.     top:0;
  17.     left:0;
  18. }
  19. .control_bar_cursor:hover{
  20.     background:white;
  21. }
  22. #control_bar_mask{
  23.     margin-top:-203px;
  24.     width:100px;
  25. }
复制代码
这里为了显示遮罩效果把遮罩层的div宽度设小了


3. 添加js
js
  1. window.onload = function(){
  2.     var control_bar = document.getElementsByClassName("control_bar")[0];
  3.     var control_bar_mask = document.getElementById("control_bar_mask");
  4.     var control_bar_cursor = document.getElementsByClassName("control_bar_cursor")[0];
  5.     var def_left = control_bar_cursor.offsetLeft;
  6.     document.body.onmousedown = function(){
  7.         window.onmousemove = function(){
  8.             var cursor_X = event.clientX;
  9.             var cursor_Y = event.clientY;
  10.             if(cursor_X < def_left){
  11.                 control_bar_cursor.style.left = 0;
  12.             }else if(cursor_X > control_bar.offsetWidth + def_left){
  13.                 control_bar_cursor.style.left = control_bar.offsetWidth;
  14.             }else{
  15.                 control_bar_cursor.style.left = cursor_X - def_left + "px";
  16.             }
  17.             var proportion = parseInt(control_bar_cursor.offsetLeft - def_left) / parseInt(control_bar.offsetWidth - 1);
  18.             control_bar_mask.style.width = proportion * control_bar.offsetWidth + "px";
  19.         };
  20.         window.onmouseup = function(){
  21.             window.onmousemove = null;
  22.         };
  23.     };
  24. };
复制代码
4. 添加一个mask用控制条来控制其透明度达到亮度调节效果
  1. <div class="mask"></div>
复制代码
  1. .mask{
  2.     position:fixed;
  3.     bottom:0;
  4.     top:0;
  5.     left:0;
  6.     right:0;
  7.     background:black;
  8.     z-index:-1;
  9. }
复制代码
  1. window.onload = function(){
  2.     var control_bar = document.getElementsByClassName("control_bar")[0];
  3.     var control_bar_mask = document.getElementById("control_bar_mask");
  4.     var control_bar_cursor = document.getElementsByClassName("control_bar_cursor")[0];
  5.     var def_left = control_bar_cursor.offsetLeft;
  6.     var mask = document.getElementsByClassName("mask")[0];
  7.     document.body.onmousedown = function(){
  8.         window.onmousemove = function(){
  9.             var cursor_X = event.clientX;
  10.             var cursor_Y = event.clientY;
  11.             if(cursor_X < def_left){
  12.                 control_bar_cursor.style.left = 0;
  13.             }else if(cursor_X > control_bar.offsetWidth + def_left){
  14.                 control_bar_cursor.style.left = control_bar.offsetWidth;
  15.             }else{
  16.                 control_bar_cursor.style.left = cursor_X - def_left + "px";
  17.             }
  18.             //亮度比
  19.             var proportion = parseInt(control_bar_cursor.offsetLeft - def_left) / parseInt(control_bar.offsetWidth - 1);
  20.             control_bar_mask.style.width = proportion * control_bar.offsetWidth + "px";
  21.             mask.style.opacity = 1 - proportion;
  22.         };
  23.         window.onmouseup = function(){
  24.             window.onmousemove = null;
  25.         };
  26.     };
  27. };
复制代码
总结
到此这篇关于HTML+CSS+JS模仿win10亮度调节效果的示例代码的文章就介绍到这了,更多相关html css win10 亮度调节内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章,希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

特级红客

关注
  • 3155
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行