分享一个小案例:利用js和HTML编写的天气情况查询

简介: 分享一个小案例:利用js和HTML编写的天气情况查询

先来看看最终的效果吧!
weather

源码

HTML页面源码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>weater</title>
    <link rel="stylesheet" href="./css/style.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" charset="utf-8"></script>
</head>
<body>
    <div class="container">
        <!--左边部分-->
        <div class="weather-side">
            <!-- 渐变 -->
            <div class="gradient"></div>
            <div class="date-container">
                <h2 class="date-dayname" id="date-dayname">周二</h2>
                <span class="date-day" id="date-day">2020-3-24</span>
                <span class="iconfont" id="location">北京</span>
            </div>
            <div class="weather-conteiner">
                <!--天气图标-->
                <span class="iconfont" id="weather-1"></span>
                <div class="weather-temp" id="weather-temp">25℃</div>
                <div class="weather-desc" id="weather-desc">晴天</div>
            </div>

        </div>
        <!--右边部分-->
        <div class="info-side">
            <div class="today-info-container">
                <div class="today-info">
                    <!--PM2.5-->
                    <div class="pm clear ">
                        <span class="title">PM2.5</span>
                        <span class="value" id="pm">00</span>
                    </div>
                     <!--湿度-->
                     <div class="humidity clear">
                        <span class="title">湿度</span>
                        <span class="value" id="humidity">00</span>
                    </div> 
                     <!--风速-->
                     <div class="humidity clear">
                        <span class="title">风速</span>
                        <span class="value" id="wind">00</span>
                    </div>                                        
                </div>
            </div>
            <div class="week-container">
                <ul class="week-list">
                    <li class="active" id="day1">
                        <span class="iconfont"></span>
                        <span class="date-dayname">星期二</span>
                        <span class="day-temp" >25℃</span>
                    </li>
                    <li  id="day2">
                        <span class="iconfont"></span>
                        <span class="date-dayname">星期三</span>
                        <span class="day-temp">25℃</span>
                    </li>
                    <li  id="day3">
                        <span class="iconfont"></span>
                        <span class="date-dayname">星期四</span>
                        <span class="day-temp">25℃</span>
                    </li>                    
                    <li  id="day4">
                        <span class="iconfont"></span>
                        <span class="date-dayname">星期五</span>
                        <span class="day-temp">25℃</span>
                    </li>                    
                </ul>
            </div>
            <div class="location-container">
                <input type="text" placeholder="位置" name="" id="city">
                <button class="location-button" id="location-button">
                    <span>查询</span>
                </button>
            </div>
        </div>
    </div>
</body>
<script src="./js/index.js"></script>
</html>

CSS样式表

@font-face {
   
   
    font-family: 'iconfont';
    src: url('iconfont.eot');
    src: url('iconfont.eot?#iefix') format('embedded-opentype'),
        url('iconfont.woff2') format('woff2'),
        url('iconfont.woff') format('woff'),
        url('iconfont.ttf') format('truetype'),
        url('iconfont.svg#iconfont') format('svg');
  }

  .iconfont {
   
   
    font-family: "iconfont" !important;
    font-size: 16px;
    font-style: normal;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }


* {
   
   
    margin: 0;
    padding:0;
}

ul {
   
   
    list-style: none;
}

/*清除浮动*/
.clear::after {
   
   
    content: '';
    display: block;
    clear: both;
}

body {
   
   
    width: 100%;
    height: 100vh;
    background-color: #424242;
    display: flex;
    align-self: center;
    justify-content: center;
}

.container {
   
   
    margin-top: 10px;
    width: 750px;
    height: 435px;
    background-color: #222831;
    border-radius: 25px;
    box-shadow: 0 0 70px -10px rgba(0,0,0, 0.2);
    color:#fff
}

.weather-side {
   
   
    position: relative;
    width: 300px;
    height: 100%;
    border-radius: 25px 0 0 25px;
    box-shadow: 0 0 35px -10px rgba(0, 0, 0, 0.2);
    float: left;

}

.gradient {
   
   
    width: 100%;
    height: 100%;
    position: absolute;
    background-image: linear-gradient(135deg, #72EDF1 10%, #5151E6 100%);
    opacity: 0.8;
    position: absolute;
}
.date-container {
   
   
    position: absolute;
    top: 25px;
    left: 25px;
}

.date-container .date-day {
   
   
    display: block;
    margin-top: 4px;
}

.weather-conteiner {
   
   
    position: absolute;
    bottom: 25px;
    left: 25px;
}

#weather-1 {
   
   
    font-size: 70px;
}
.weather-temp {
   
   
    font-size: 4em;
    font-weight: bold;
}
.weather-desc {
   
   
    font-size: 20px;
}

.info-side {
   
   
    /* background: red; */
    float: right;
    padding-top: 25px;
    margin-right: 10px;
    height: 100%;
    box-sizing: border-box;
    position: relative;
}

.today-info {
   
   
    /* background-color: #5151E6; */
    padding: 15px 15px 0 15px;
    margin: 0 25px 25px 25px;
    box-shadow: 0 0 50px -5px rgba(0, 0, 0, 0.4);
    border-radius: 10px;
}
.today-info div {
   
   
    margin-bottom: 10px;
}

.today-info .title {
   
   
    float: left;
}
.today-info .value {
   
   
    float: right;
}

.week-list {
   
   
    margin: 10px 35px;
    border-radius: 10px;
    box-shadow: 0 0 50px -5px rgba(0, 0, 0, 0.4);

}

.week-list li {
   
   
    float: left;
    /* background-color: red; */
    padding: 15px;
    cursor: pointer;
    border-radius: 10px;
    text-align: center;
    transition: 200ms;
    z-index: 100;

}

.week-list li span {
   
   
    display: block;
}
.week-list .iconfont {
   
   
    font-size: 36px;
}

.week-list li:hover {
   
   
    transform: scale(1.1);
    background-color: #ffffff;
    color: #222831;
}
.active {
   
   
    background-color: #ffffff;
    color: #222831;
}

.week-list .day-name {
   
   
    margin-top: 10px;
    text-align: center;
}

.week-list .day-temp {
   
   
    margin-top: 10px;
    text-align: center;
    font-size: 12px;
    font-weight: bold;
}

.location-container {
   
   
    padding: 25px 35px;
    position: relative;
    /* background-color: red; */
}

.location-container input {
   
   
    outline: none;
    width: 300px;
    height: 28px;
    padding: 6px;
    display: block;
    box-sizing: border-box;
    position: absolute;
    bottom: -135px;
    left: calc(50%-150px);
    border: none;
    color: #222831;
    border-radius: 5px;
}

.location-container button {
   
   
    outline: none;
    width: 320px;
    padding: 8px;
    box-sizing: border-box;
    border: none;
    color: #ffffff;
    background-image: linear-gradient(135deg, #72EDF1 10%, #5151E6 100%);
    border-radius: 25px;
    font-weight: bold;
    box-shadow: 0 0 40px -5px rgba(0, 0, 0, 0.3);
    cursor: pointer;
    transition: 200ms;
    position: absolute;
    bottom: -195px;
    left: calc(50% - 160px);

}

.location-container button:hover {
   
   
    transform: scale(1.05);
}

JS脚本源码

function getweather(city){
   
   
    $.ajax({
   
   
        type: 'GET',
        dataType: 'jsonp',
        async: false,
        url: 'http://api.asilu.com/weather/?callback=getname&city='+city,
        success:function(data){
   
   
          weather(data)
        }
    })
}    

function weather(data){
   
   
    console.log(data)
    $("#date-day")[0].innerHTML = data.date
    $("#date-dayname")[0].innerHTML = data.weather[0].date.slice(0,3)
    $("#pm")[0].innerHTML = data.pm25
    $("#humidity")[0].innerHTML = "暂无"
    $("#wind")[0].innerHTML = data.weather[0].wind
    $("#location")[0].innerHTML = data.city
    $("#weather-temp")[0].innerHTML = data.weather[0].temp.slice(0,3) + "℃";
    $("#weather-desc")[0].innerHTML = data.weather[0].weather
    $("#weather-1")[0].innerHTML = weatherIcon(data,1);

    var day1 = $("#day1")
    var span1 =$(day1).find('span') 
    var day2 = $("#day2")
    var span2 =$(day2).find('span') 
    var day3 = $("#day3")
    var span3 =$(day3).find('span') 
    var day4 = $("#day4")
    var span4 =$(day4).find('span')  

    span1[0].innerHTML = weatherIcon(data,1);
    span1[1].innerHTML = data.weather[0].date.slice(0,3);
    span1[2].innerHTML = data.weather[0].temp;

    span2[0].innerHTML = weatherIcon(data,2);
    span2[1].innerHTML = data.weather[1].date
    span2[2].innerHTML = data.weather[1].temp;

    span3[0].innerHTML = weatherIcon(data,3);
    span3[1].innerHTML = data.weather[2].date
    span3[2].innerHTML = data.weather[2].temp;

    span4[0].innerHTML = weatherIcon(data,4);
    span4[1].innerHTML = data.weather[3].date
    span4[2].innerHTML = data.weather[3].temp;

}
function weatherIcon(data,t) {
   
   

    if (data.weather[t - 1].icon1.search("yin") != -1) {
   
   
        return "&#xe608;";
    }
    if(data.weather[t - 1].icon1.search("duoyun") != -1){
   
   
            return "&#xe607;";
        }
    if(data.weather[t - 1].icon1.search("qing") != -1){
   
   
            return "&#xe600;";
    }
    if(data.weather[t - 1].icon1.search("xiaoyu") != -1){
   
   
            return "&#xe626;";
    }
    if(data.weather[t - 1].icon1.search("zhongyu") != -1){
   
   
            return "&#xe667;";
    }
    if(data.weather[t - 1].icon1.search("dayu") != -1 || data.weather[0].icon1.search("baoyu") != -1){
   
   
            return "&#xe610;";
    }
    if(data.weather[t - 1].icon1.search("zhenyu") != -1){
   
   
            return "&#xe701;";
    }    
}

$("#location-button").click(function(){
   
   
    var city=$("#city").val()
    getweather(city)
})

window.onload = function() {
   
   
    this.getweather('北京')
}
目录
相关文章
|
6天前
|
前端开发 JavaScript
用HTML CSS JS打造企业级官网 —— 源码直接可用
必看!用HTML+CSS+JS打造企业级官网-源码直接可用,文章代码仅用于学习,禁止用于商业
35 1
|
11天前
|
前端开发 JavaScript 安全
HTML+CSS+JS密码灯登录表单
通过结合使用HTML、CSS和JavaScript,我们创建了一个带有密码强度指示器的登录表单。这不仅提高了用户体验,还帮助用户创建更安全的密码。希望本文的详细介绍和代码示例能帮助您在实际项目中实现类似功能,提升网站的安全性和用户友好性。
22 3
|
14天前
|
JavaScript
JS鼠标框选并删除HTML源码
这是一个js鼠标框选效果,可实现鼠标右击出现框选效果的功能。右击鼠标可拖拽框选元素,向下拖拽可实现删除效果,简单实用,欢迎下载
27 4
|
13天前
|
移动开发 HTML5
html5+three.js公路开车小游戏源码
html5公路开车小游戏是一款html5基于three.js制作的汽车开车小游戏源代码,在公路上开车网页小游戏源代码。
39 0
html5+three.js公路开车小游戏源码
|
22天前
|
JSON 移动开发 数据格式
html5+css3+js移动端带歌词音乐播放器代码
音乐播放器特效是一款html5+css3+js制作的手机移动端音乐播放器代码,带歌词显示。包括支持单曲循环,歌词显示,歌曲搜索,音量控制,列表循环等功能。利用json获取音乐歌单和歌词,基于html5 audio属性手机音乐播放器代码。
73 6
|
1月前
|
XML 前端开发 JavaScript
前端开发进阶:从HTML到React.js
【10月更文挑战第9天】前端开发进阶:从HTML到React.js
|
1月前
|
JavaScript 前端开发
电话号码正则表达式 代码 javascript+html,JS正则表达式判断11位手机号码
电话号码正则表达式 代码 javascript+html,JS正则表达式判断11位手机号码
102 1
|
1月前
|
JavaScript 前端开发
JavaScript 与 HTML 的结合
JavaScript 与 HTML 的结合
15 0
|
4月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的客户关系管理系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的客户关系管理系统附带文章源码部署视频讲解等
96 2
|
4月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的小区物流配送系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的小区物流配送系统附带文章源码部署视频讲解等
123 4