在一些小游戏界面,都会出现一些开关灯的按钮,那么这些效果是如何做出来的呢,今天我们就来探讨一下,其实制作这些效果非常的简单,就是利用了js操作DOM的特性,通过点击跟换样式来实现,源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body {
transition: all .6s;
}
#left-fixed {
position: fixed;
/* left: -275px; */
top: 20%;
width: 300px;
padding: 20px;
height: auto;
border-radius: 5px;
background-color: #ccc;
transition: all .6s;
}
#left-fixed:hover {
left: 0;
background-color: pink;
}
* {
margin: 0;
padding: 0;
}
img {
border: 0;
}
ol,
ul,
li {
list-style: none;
}
h4 {
text-align: center;
font-size: 40px;
margin: 20px 0;
text-shadow: 0 0 10px #ccc;
}
</style>
</head>
<body>
<h4>您可以点击切换桌面</h4>
<div id="left-fixed">
<!-- <input type="submit" id="switchGo" value="关灯"> -->
<label for="inputs">改变背景色:</label>
<input type="color" class="form-control" id="inputs">
<br />
<button type="" class="form-control" id="buttons">确定</button>
<div>
<label for="linear-deg">改变小盒子的渐变角度</label>
<input type="number" class="form-control" id="linear-deg" max="360" placeholder="输入的数值大于360无效">
<label for="linear-bgc">改变小盒子的渐变颜色(1)</label>
<input type="color" class="form-control" id="linear-bgc">
<label for="linear-bgc1">改变小盒子的渐变颜色(2)</label>
<input type="color" class="form-control" id="linear-bgc1">
<br />
<button type="" class="form-control" id="btn-linear-bgc">确定</button>
<br>
<input type="submit" class="form-control" name="" value="点击清空" onclick="window.location.href=''">
</div>
</div>
</body>
</html>
<script type="text/javascript">
/*开关灯--开始*/
/* var switchGo = document.getElementById("switchGo");
var i = -1;
switchGo.onclick = function() {
i++;
if(i == 0) {
document.body.style.background = "#000";
this.value = "开灯";
}
if(i == 1) {
document.body.style.background = "#fff";
this.value = "关灯";
i = -1;
}
}*/
/*开关灯--结束*/
/*这里是渐变部分的实现--开始*/
var linearBgc = document.getElementById("linear-bgc");
var linearBgc1 = document.getElementById("linear-bgc1");
var btnLinearBgc = document.getElementById("btn-linear-bgc");
var linearDeg = document.getElementById("linear-deg");
var h4s = document.getElementsByTagName("h4");
/*单种颜色的切换*/
var buttons=document.getElementById("buttons");
var colorOne="#000";
inputs.onmouseout=function(){
//document.body.style.backgroundColor=colorOne;
colorOne=this.value;
//alert(colorOne);
//弹窗测试是否有效果
}
buttons.onclick=function(){
document.body.style.background = "linear-gradient(" + 0 + "deg" + "," + colorOne + "," + colorOne + ")";
}
btnLinearBgc.onclick = function() {
var color = linearBgc.value;
var color1 = linearBgc1.value;
var numberDeg = linearDeg.value;
console.log(numberDeg);
boxColor(numberDeg, color, color1);
}
function boxColor(numberDeg, color, color1) {
for(var i = 0; i < h4s.length; i++) {
if(numberDeg == "")
/**/
h4s[i].parentNode.style.background = "linear-gradient(" + color + "," + color1 + ")";
else
h4s[i].parentNode.style.background = "linear-gradient(" + numberDeg + "deg" + "," + color + "," + color1 + ")";
}
}
</script> |
如上就是本期的内容了,希望大家能够学到!
「梦想一旦被付诸行动,就会变得神圣,如果觉得我的文章对您有用,请帮助本站成长」
共 0 条评论关于"js开灯关灯效果_增强版"
最新评论