“便携小空调”曾经火遍朋友圈与空间,今天我们就用web来打造一个属于自己的夏日小空调吧!
下面是效果图:
这是点击“独立遥控器”后的效果:
这是空调打开后的效果:
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
*{
margin: 0;
padding: 0;
}
div.air{
position: relative;
}
h1.name{
font-size: 25px;
text-align: center;
}
div.light img{
width: 20px;
height: 20px;
float: left;
margin-left: 550px;
/* margin-top: 20px; */
}
div.light{
margin-top: 20px;
}
div.light span.two{
color: rgb(13, 117, 236);
}
div.box{
width: 45%;
height: 150px;
margin: 20px auto;
border: 1px solid gray;
border-radius: 20px;
}
div.box hr{
border: 2px solid gray;
}
div.box img.blue{
width: 45px;
height: 60px;
float: left;
margin-left: 10px;
margin-top: 10px;
}
div.box img.green{
width: 45px;
height: 45px;
margin-top: 10px;
float: left;
}
div.box img.pig{
margin-top: 100px;
margin-left: 170px;
width: 30px;
height: 15px;
}
div.box div#circle{
width: 5px;
height: 5px;
border-radius: 50%;
background-color: gray;
float: right;
margin-right: 10px;
margin-top: 4px;
}
div.box span#public{
color: gray;
font-size: 30px;
float: right;
margin-top: 30px;
margin-right: 20px;
display: none;
}
div.box img#hothot,img#coldcold{
width: 15px;
height: 15px;
position: absolute;
right:425px;
top: 110px;
display: none;
}
div.flay{
/* background-color: aqua; */
margin: 0 auto;
width: 300px;
height: 70px;
}
div.flay img{
width: 300px;
height: 70px;
display: none;
}
div.air button{
border: 1px solid rgb(9, 70, 211);
color:rgb(9, 70, 211);
width: 100px;
height: 30px;
margin-left: 590px;
cursor: pointer;
}
div.air div.line img{
width: 60px;
height: 60px;
margin: 20px 15px;
cursor: pointer;
}
div.air div.line{
text-align: center;
}
div.air div.up>img,div.down>img{
width: 60px;
height: 60px;
cursor: pointer;
}
div.air div.up,div.down{
text-align: center;
}
div.line img#guan{
display: none;
}
div.switch{
display: inline-block;
}
</style>
<body>
<div class="air">
<h1 class="name">便携小空调</h1>
<div class="light">
<img src="./light.png">
<span class="one">Tip:为你的夏日带去</span>
<span class="two">清凉!</span>
</div>
<!-- 空调主题框架 -->
<div class="box">
<img src="./blue.png" class="blue">
<img src="./green.png" class="green">
<img src="./coldcold.png" id="coldcold">
<img src="./hothot.png" id="hothot">
<span id="public"><span id="num"></span><span id="du">°C</span></span>
<img src="./pig.png" class="pig">
<hr/>
<div id="circle"></div>
</div>
<div class="flay"><img id="flay" src="./flay.png"></div>
<!-- 独立遥控器 -->
<button onclick="independent()">独立遥控器</button>
<!-- 空调制冷按钮、开关、制热按钮 -->
<div class="line">
<img src="./cold.png" onclick="cold()" class="cold">
<div class="switch">
<img src="./open.png" onclick="fun()" id="kai">
<img src="./close.png" onclick="hide()" id="guan">
</div>
<img src="./hot.png" onclick="hot()" class="hot">
</div>
<!-- 空调温度调动按钮 -->
<div class="up" onclick="up()"><img src="./up.png"></div>
<div class="down" onclick="down()"><img src="./down.png"></div>
</div>
</body>
<script>
let bol;
// 开关打开时执行
function fun(){
bol=true;
document.getElementById("public").style="display:block";
document.getElementById("kai").style="display:none";
document.getElementById("guan").style="display:block";
document.getElementById("flay").style="display:block";
document.getElementById("circle").style="background-color:greenyellow";
document.getElementById("coldcold").style="display:block";
num();
}
// 开关关闭时执行
function hide(){
bol=false;
document.getElementById("kai").style="display:block";
document.getElementById("guan").style="display:none";
document.getElementById("flay").style="display:none";
document.getElementById("circle").style="background-color: gray";
document.getElementById("public").style="display:none";
document.getElementById("hothot").style="display:none";
document.getElementById("coldcold").style="display:none";
}
// 空调温度的显示,定义初始温度为25度
let i;
function num(){
i=25;
if(bol==true){
document.getElementById("num").innerHTML=i;
return;
}
}
// 空调温度上调时执行
function up(){
if(bol==true){
i++;
if(i>30){
alert("温度最高为30度");
return;
}
document.getElementById("num").innerHTML=i;
}else{
alert("请先打开空调");
}
}
// 空调温度下调时执行
function down(){
if(bol==true){
i--;
if(i<16){
alert("温度最低为16度");
return;
}
document.getElementById("num").innerHTML=i;
}else{
alert("请先打开空调");
}
}
// 打开空调温度制冷按钮时执行
function cold(){
if(bol==true){
document.getElementById("coldcold").style="display:block";
document.getElementById("hothot").style="display:none";
}
}
// 打开空调温度制热按钮时执行
function hot(){
if(bol==true){
document.getElementById("hothot").style="display:block";
document.getElementById("coldcold").style="display:none";
}
}
// 点击空调“独立遥控器”时执行
function independent(){
open("http://127.0.0.1:5500/day14/air2.html","独立遥控
器","width=400px,height=300px,left=300px,top=200px");
}
</script>
</html>
下面是点击“独立遥控器”时打开的另一个网页的代码(这段代码只是实现了另一个网页的样式,其功能还没有实现,在稍后的博客中我会加以改进):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
*{
margin: 0;
padding: 0;
}
div.line img{
width: 60px;
height: 60px;
margin: 20px 15px;
cursor: pointer;
}
div.line{
text-align: center;
}
div.up>img,div.down>img{
width: 60px;
height: 60px;
cursor: pointer;
}
div.up,div.down{
text-align: center;
}
div.line img#guan{
display: none;
}
div.switch{
display: inline-block;
}
</style>
<body>
<div class="line">
<img src="./cold.png" onclick="cold()" class="cold">
<div class="switch">
<img src="./open.png" onclick="fun()" id="kai">
<img src="./close.png" onclick="hide()" id="guan">
</div>
<img src="./hot.png" onclick="hot()" class="hot">
</div>
<div class="up" onclick="up()"><img src="./up.png"></div>
<div class="down" onclick="down()"><img src="./down.png"></div>
</body>
<script>
let bol;
function fun(){
bol=true;
document.getElementById("public").style="display:block";
document.getElementById("kai").style="display:none";
document.getElementById("guan").style="display:block";
document.getElementById("flay").style="display:block";
document.getElementById("circle").style="background-color:greenyellow";
document.getElementById("coldcold").style="display:block";
num();
}
function hide(){
bol=false;
document.getElementById("kai").style="display:block";
document.getElementById("guan").style="display:none";
document.getElementById("flay").style="display:none";
document.getElementById("circle").style="background-color: gray";
document.getElementById("public").style="display:none";
document.getElementById("hothot").style="display:none";
document.getElementById("coldcold").style="display:none";
}
let i;
function num(){
i=25;
if(bol==true){
document.getElementById("num").innerHTML=i;
return;
}
}
function up(){
if(bol==true){
i++;
if(i>30){
alert("温度最高为30度");
return;
}
document.getElementById("num").innerHTML=i;
}else{
alert("请先打开空调");
}
}
function down(){
if(bol==true){
i--;
if(i<16){
alert("温度最低为16度");
return;
}
document.getElementById("num").innerHTML=i;
}else{
alert("请先打开空调");
}
}
function cold(){
if(bol==true){
document.getElementById("coldcold").style="display:block";
document.getElementById("hothot").style="display:none";
}
}
function hot(){
if(bol==true){
document.getElementById("hothot").style="display:block";
document.getElementById("coldcold").style="display:none";
}
}
</script>
</html>
好了,以上就是用web制作的“便携小空调”了,一起来试试吧!!!