开关灯案例
原创
©著作权归作者所有:来自51CTO博客作者不秃头的小李同学的原创作品,请联系作者获取转载授权,否则将追究法律责任
效果图:
源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
background-color: white;
}
div {
height: 200px;
width: 200px;
margin:100px auto;
}
button {
width: 100%;
height: 100%;
background: url(image/black.png) no-repeat;
border: 0px;
outline: 0px;
}
</style>
</head>
<body>
<div>
<button>
</button>
</div>
<script>
var body = document.querySelector('body');
var bt = document.querySelector('button');
var flag = 1;
bt.onclick = function () {
if(flag == 1){
this.style.background='url(image/white.png)';
body.style.background='black';
flag = 0;
}
else {
this.style.background='url(image/black.png)';
body.style.background='white';
flag = 1;
}
}
</script>
</body>
</html>
素材:
白灯:
可能因为CSDN的背景色看不出来
黑灯: