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

所属分类: 网页制作 / HTML/Xhtml 阅读数: 1938
收藏 0 赞 0 分享

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>
		<div class="mask"></div>
		<div class="control_bar"></div>
		<div class="control_bar" style="border-bottom:3px solid #505151;" id="control_bar_mask"></div>
		<div class="control_bar_cursor"></div>
	</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

<div class="control_bar">
</div>
<div class="control_bar" style="border-bottom:3px solid #505151;"  
id="control_bar_mask>
</div>
<div class="control_bar_cursor">
</div>

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;
}

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

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用控制条来控制其透明度达到亮度调节效果

<div class="mask"></div>
.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+CSS+JS模仿win10亮度调节效果的示例代码的文章就介绍到这了,更多相关html css win10 亮度调节内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

更多精彩内容其他人还在看

网页注释在IE中产生文字溢出

实验代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transiti
收藏 0 赞 0 分享

HTML教程:定义列表

原文:http://andymao.com/andy/post/104.html 上一节:有序列表 写完“无序列表”和“有序列表”之后已经有人和我说这两篇看得没什么意思。这两篇文章如果只以单向读取的形式阅读那么的确是没什
收藏 0 赞 0 分享

HTML教程:有序列表

原文:http://andymao.com/andy/post/103.html 上一节:无序列表 信息有时候是无序归纳的,有的却有着明确的顺序,在上一篇也提到了。那么简单的来想一下身边有哪些事物是有先后顺序的:操作步骤、排行榜、书目录……
收藏 0 赞 0 分享

HTML教程:无序列表

原文:http://andymao.com/andy/post/102.html 段落已经讲完了,那么一些基本的应用方式也讲了一些,那么是否已经应用了呢?当然应用可以更为丰富,那么这些就需要自己在实际工作中不断的摸索与思考,然后创新并总结得出新的应用形式。当然了段落不能当作
收藏 0 赞 0 分享

HTML网页制作的强大8条技巧

  虽然现在有许多网页制作工具能让您轻松地完成工作,但如果使用HTML则可以得到更大控制权,下面介绍几个小技巧。   1。使用<tt>,<i>,<br>语句来控制文字排版比用<pre>好得多。 如: <tt>实用
收藏 0 赞 0 分享

网页表格分割线去除方法

网页表格分割线去除方法。 其实上面的三个表格都有三行三列,隐藏分隔线的诀窍在于rules,察看这三个表格的源代码,我们可以看到<TABLE>标签中都有rules。它有三个参数(cols,rows,none),当rules=cols时,表格会隐藏纵向的分隔线,这
收藏 0 赞 0 分享

blockquote标记应用注意

关于语义化,不是一句两句就能说明白的,而且现在也没有一个官方的很严格的定义。关于<blockquote>没有争议的是: 1.引用一段较长的文字 2.可以使用cite标签或者属性 问题是<blockquote>引用的文字必须使用块级元素将他
收藏 0 赞 0 分享

网页表格表框制作技巧

网页表格表框制作技巧。 -------------------------------------------------------------------------------- 表格边框的显示与隐藏,是可以用frame参数来控制的。请注意它只控制表格的边框图,而不
收藏 0 赞 0 分享

HTML其实就是学习几个重要标记的应用

《这将是一场革命》一文出来以后。得到业界大伙的认同,当然与此同时也得到部分来自内部与外部的挑衅,不过的更加多的是更多人来寻问每一个点的细节。今晚回家很早就睡了,半夜在一个梦中醒来,梦里正在和小学的同学玩一个游戏——“The Next&rdquo
收藏 0 赞 0 分享

移动端专用的meta标签设置大全

不知道有没有人觉得,html的meta标签描述的头部信息特别多,下面这篇文章主要给大家分享介绍了关于移动端专用的meta设置的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多