炎炎夏日打造一个属于自己的“便携小空调”吧

本文介绍了如何使用HTML、CSS和JavaScript创建一个互动的Web版‘便携小空调’,包括空调的开关、温度调节、制冷制热等功能。通过点击‘独立遥控器’,还能弹出新的网页进行远程控制。代码详细展示了其实现过程,为初学者提供了实践Web交互设计的实例。
摘要由CSDN通过智能技术生成

“便携小空调”曾经火遍朋友圈与空间,今天我们就用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制作的“便携小空调”了,一起来试试吧!!!

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值