登录  /  注册

HTML+CSS+JS 模仿 Win10 亮度调节效果

Guanhui
发布: 2020-07-18 18:00:40
转载
2513人浏览过


HTML+CSS+JS 模仿 Win10 亮度调节效果

HTML+CSS+JS模仿win10亮度调节效果

代码

<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<title>模仿win10的亮度调节</title>
		<style>
			.control_bar{
				height:200px;
				width:500px;
				border-bottom:3px solid #888888;
				
			}
			.control_bar_cursor{
				height:25px;
				width:8px;
				background: #505151;
				border-radius:5px;
				margin-top:-12.5px;
				position:relative;
				top:0;
				left:0;
			}
			.control_bar_cursor:hover{
				background:white;
			}
			#control_bar_mask{
				margin-top:-203px;
				width:0px;
			}
			.mask{
				position:fixed;
				bottom:0;
				top:0;
				left:0;
				right:0;
				background:black;
				z-index:-1;
			}
		</style>
	</head>
	<body>
		<p class="mask"></p>
		<p class="control_bar"></p>
		<p class="control_bar" style="border-bottom:3px solid #505151;" id="control_bar_mask"></p>
		<p class="control_bar_cursor"></p>
	</body>
	<script>
		window.onload = function(){
			var control_bar = document.getElementsByClassName("control_bar")[0];
			var control_bar_mask = document.getElementById("control_bar_mask");
			var control_bar_cursor = document.getElementsByClassName("control_bar_cursor")[0];
			var def_left = control_bar_cursor.offsetLeft;
			var mask = document.getElementsByClassName("mask")[0];
			document.body.onmousedown = function(){
				window.onmousemove = function(){
					var cursor_X = event.clientX;
					var cursor_Y = event.clientY;
					if(cursor_X < def_left){
						control_bar_cursor.style.left = 0;
					}else if(cursor_X > control_bar.offsetWidth + def_left){
						control_bar_cursor.style.left = control_bar.offsetWidth;
					}else{
						control_bar_cursor.style.left = cursor_X - def_left + "px";
					}
					//亮度比
					var proportion = parseInt(control_bar_cursor.offsetLeft - def_left) / parseInt(control_bar.offsetWidth - 1);
					control_bar_mask.style.width = proportion * control_bar.offsetWidth + "px";
					mask.style.opacity = 1 - proportion;
					};
				window.onmouseup = function(){
					window.onmousemove = null;
				};
			};
		};
	</script>
</html>
登录后复制

1.将各个元素的样子写出来

这里为了方便好观察给body添加了一个背景颜色

html

<p class="control_bar">
</p>
<p class="control_bar" style="border-bottom:3px solid #505151;"  
id="control_bar_mask>
</p>
<p class="control_bar_cursor">
</p>
登录后复制

css

body{
    background:back;
}
.control_bar{
    height:200px;
    width:500px;
    border-bottom:3px solid #888888;
}
.control_bar_cursor{
    height:25px;
    width:8px;
    background: #505151;
    border-radius:5px;
}
登录后复制

效果图

2. 将各个元素叠到一起

css

body{
    background:black;
}
.control_bar{
    height:200px;
    width:500px;
    border-bottom:3px solid #888888;

}
.control_bar_cursor{
    height:25px;
    width:8px;
    background: #505151;
    border-radius:5px;
    margin-top:-12.5px;
    position:relative;
    top:0;
    left:0;
}
.control_bar_cursor:hover{
    background:white;
}
#control_bar_mask{
    margin-top:-203px;
    width:100px;
}
登录后复制

这里为了显示遮罩效果把遮罩层的p宽度设小了

3. 添加js

js

window.onload = function(){
    var control_bar = document.getElementsByClassName("control_bar")[0];
    var control_bar_mask = document.getElementById("control_bar_mask");
    var control_bar_cursor = document.getElementsByClassName("control_bar_cursor")[0];
    var def_left = control_bar_cursor.offsetLeft;
    document.body.onmousedown = function(){
        window.onmousemove = function(){
            var cursor_X = event.clientX;
            var cursor_Y = event.clientY;
            if(cursor_X < def_left){
                control_bar_cursor.style.left = 0;
            }else if(cursor_X > control_bar.offsetWidth + def_left){
                control_bar_cursor.style.left = control_bar.offsetWidth;
            }else{
                control_bar_cursor.style.left = cursor_X - def_left + "px";
            }
            var proportion = parseInt(control_bar_cursor.offsetLeft - def_left) / parseInt(control_bar.offsetWidth - 1);
            control_bar_mask.style.width = proportion * control_bar.offsetWidth + "px";
        };
        window.onmouseup = function(){
            window.onmousemove = null;
        };
    };
};
登录后复制

4. 添加一个mask用控制条来控制其透明度达到亮度调节效果

<p class="mask"></p>
登录后复制
.mask{
    position:fixed;
    bottom:0;
    top:0;
    left:0;
    right:0;
    background:black;
    z-index:-1;
}
登录后复制
window.onload = function(){
    var control_bar = document.getElementsByClassName("control_bar")[0];
    var control_bar_mask = document.getElementById("control_bar_mask");
    var control_bar_cursor = document.getElementsByClassName("control_bar_cursor")[0];
    var def_left = control_bar_cursor.offsetLeft;
    var mask = document.getElementsByClassName("mask")[0];
    document.body.onmousedown = function(){
        window.onmousemove = function(){
            var cursor_X = event.clientX;
            var cursor_Y = event.clientY;
            if(cursor_X < def_left){
                control_bar_cursor.style.left = 0;
            }else if(cursor_X > control_bar.offsetWidth + def_left){
                control_bar_cursor.style.left = control_bar.offsetWidth;
            }else{
                control_bar_cursor.style.left = cursor_X - def_left + "px";
            }
            //亮度比
            var proportion = parseInt(control_bar_cursor.offsetLeft - def_left) / parseInt(control_bar.offsetWidth - 1);
            control_bar_mask.style.width = proportion * control_bar.offsetWidth + "px";
            mask.style.opacity = 1 - proportion;
        };
        window.onmouseup = function(){
            window.onmousemove = null;
        };
    };
};
登录后复制

推荐教程:《HTML教程

以上就是HTML+CSS+JS 模仿 Win10 亮度调节效果的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:jb51网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号