HTML+CSS+JS模仿win10亮度調(diào)節(jié)效果的示例代碼
HTML+CSS+JS模仿win10亮度調(diào)節(jié)效果
代碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>模仿win10的亮度調(diào)節(jié)</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.將各個(gè)元素的樣子寫出來
這里為了方便好觀察給body添加了一個(gè)背景顏色
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. 將各個(gè)元素疊到一起
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寬度設(shè)小了

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. 添加一個(gè)mask用控制條來控制其透明度達(dá)到亮度調(diào)節(jié)效果
<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;
};
};
};
總結(jié)
到此這篇關(guān)于HTML+CSS+JS模仿win10亮度調(diào)節(jié)效果的示例代碼的文章就介紹到這了,更多相關(guān)html css win10 亮度調(diào)節(jié)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章

HTML img標(biāo)簽和超鏈接標(biāo)簽詳細(xì)介紹
文章介紹了HTML中img標(biāo)簽的使用,包括src屬性(指定圖片路徑)、相對(duì)/絕對(duì)路徑區(qū)別、alt替代文本、title提示、寬高控制及邊框設(shè)置等,本文主要給大家介紹HTML img標(biāo)簽和超鏈2025-06-20HTML中meta標(biāo)簽的常見使用案例(示例詳解)
HTML meta標(biāo)簽用于提供文檔元數(shù)據(jù),涵蓋字符編碼、SEO優(yōu)化、社交媒體集成、移動(dòng)設(shè)備適配、瀏覽器控制及安全隱私設(shè)置,優(yōu)化頁面顯示與搜索引擎索引,本文給大家介紹HTML中meta2025-06-20
input 標(biāo)簽主要用于接收用戶的輸入,隨 type 屬性值的不同,變換其具體功能,本文通過實(shí)例圖文并茂的形式給大家介紹HTML input 標(biāo)簽,感興趣的朋友一起看看吧2025-06-20
html 滾動(dòng)條滾動(dòng)過快會(huì)留下邊框線的解決方案
這篇文章主要介紹了html 滾動(dòng)條滾動(dòng)過快會(huì)留下邊框線的解決方案,解決方法很簡(jiǎn)單,可以將 dialog 單獨(dú)拿出來別放在 transform 的子元素里,需要的朋友可以參考下2025-06-09- 本文將介紹如何使用<img>標(biāo)簽在 HTML 中添加圖片,并展示一些常見的用法和技巧,通過本文的介紹,應(yīng)該掌握了在 HTML 中添加和調(diào)整圖片的基礎(chǔ)知識(shí),感興趣的朋友一起看2025-05-16
- HTML表格用于在網(wǎng)頁上展示數(shù)據(jù),通過標(biāo)簽及其相關(guān)標(biāo)簽來創(chuàng)建,表格由行和列組成,每一行包含一個(gè)或多個(gè)單元格,單元格可以包含文本、圖像、鏈接等元素,本文將詳細(xì)介紹HTML表格2025-03-12
- 本文介紹了三種禁止HTML頁面滾動(dòng)的方法:通過CSS的overflow屬性、使用JavaScript的滾動(dòng)事件監(jiān)聽器以及使用CSS的position:fixed屬性,每種方法都有其適用場(chǎng)景和優(yōu)缺點(diǎn),感興2025-02-24

使用HTML和CSS實(shí)現(xiàn)文字鏤空效果的代碼示例
在 Web 開發(fā)中,文本的視覺效果是提升用戶體驗(yàn)的重要因素之一,通過 CSS 技巧,我們可以創(chuàng)造出許多獨(dú)特的效果,例如文字鏤空效果,本文將帶你一步一步實(shí)現(xiàn)一個(gè)簡(jiǎn)單的文字鏤空2024-11-17
Html去除a標(biāo)簽的默認(rèn)樣式的操作代碼
在Html中,a標(biāo)簽?zāi)J(rèn)的超鏈接樣式是藍(lán)色字體配下劃線,這可能不滿足所有設(shè)計(jì)需求,如需去除這些默認(rèn)樣式,可以通過CSS來實(shí)現(xiàn),本文給大家介紹Html去除a標(biāo)簽的默認(rèn)樣式的操作代碼2024-09-25HTML文本域如何設(shè)置為禁止用戶手動(dòng)拖動(dòng)
在HTML中,可以通過設(shè)置CSS的resize屬性為none,來禁止用戶手動(dòng)拖動(dòng)文本域(textarea)的大小,這種方法簡(jiǎn)單有效,適用于大多數(shù)現(xiàn)代瀏覽器,但需要在老舊瀏覽器中進(jìn)行測(cè)試以確保2024-09-25





