网页中加入当前天气(附代码)

现在有很多天气网站提供了免费或收费的api,如果每日访问量不高的话可以选择免费的,下面便是使用和风天气的api制作的一个显示当前地区当前时间的天气的网页,当然如果需要天气预报等也只需要从获取的json数组里提取就是了。

上图一张:
这里写图片描述

完整代码下载及演示链接:https://codepen.io/yinyoupoet/pen/PjgWWa

界面比较简陋,重点只在于后台获取当前位置与当前天气。

获取当前经纬度代码如下

if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(function(position){
            lat = position.coords.latitude;     //维度
            lon = position.coords.longitude;    //经度
            });
     }

根据经纬度获取当前天气代码如下,免费注册后会获得一个免费的key,json格式请见官网。

var para = "https://free-api.heweather.com/v5/weather?city="+lon+","+lat+"&key=53153e2fed574ce19f5c089a1aacede0";
            console.log(para);
            $.getJSON(para,function(json){
                var city= json["HeWeather5"][0]["basic"]["city"];
                var cnty= json["HeWeather5"][0]["basic"]["cnty"];
                var weatherNow = json["HeWeather5"][0]["now"]["cond"]["txt"];
                degree=json["HeWeather5"][0]["now"]["tmp"];

最后放上完整代码:
html

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>local weather</title>
  <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'>
      <link rel="stylesheet" href="css/style.css">


</head>

<body>

    <div class="container-fluid">
        <div class="content">
            <!-- <img src="http://file.digitaling.com/eImg/uimages/20150902/1441167475585128.gif"> -->
            <div class="city">
                <img src="img/Location.png">
                <div id="cityname" style="display: inline;">城市</div>
            </div>
            <div class="weather">天气</div>
            <div class="degree">
                <div class="tmp">30</div>
                <div class="tmp-kind">°C</div>
                <div class="tmp-change"><button class="btn-tmp-change btn btn-primary">Change °C or °F</button></div>
            </div>
            <div class="wind">北风3级</div>

        </div>
    </div>

    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    <script src="js/index.js"></script>

</body>
</html>

css

*{
    margin:0;
    padding:0;
}

body{
    overflow:none;
    background: url("https://images.unsplash.com/photo-1465311530779-5241f5a29892?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=") no-repeat center center fixed;
    background-size: cover;
}

/*
    雷电:https://images.unsplash.com/photo-1431440869543-efaf3388c585?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=
    雪:https://images.unsplash.com/photo-1483119624769-b1a73c256500?dpr=1&auto=format&fit=crop&w=1080&h=724&q=80&cs=tinysrgb&crop=
    太阳:https://images.unsplash.com/photo-1472752112832-519166b23dfa?dpr=1&auto=format&fit=crop&w=1080&h=720&q=80&cs=tinysrgb&crop=
    雨:https://images.unsplash.com/photo-1417008914239-59b898b49382?dpr=1&auto=format&fit=crop&w=1080&h=708&q=80&cs=tinysrgb&crop=
*/

.content{
    position: absolute;
    margin-top: 10%;
    margin-left: 20%;
    margin-right: 20%;
    width: 60%;

}
.city{
    text-align: center;
}
.city>img{
    width: 30px;
}
.city>div{
    font-size: 28px;
    vertical-align: middle;
    color: rgb(255,102,102);
}
.weather{
    text-align: center;
    color: rgb(255,102,102);
    font-size: 38px;
}
.degree{
    text-align: center;
    color: rgb(255,102,102);
    font-size: 38px;
}
.degree div{
    display: inline;
}
.degree .tmp-kind{
    margin-left: 10px;
}
.degree .tmp-change{
    margin-left: 30px;
}
.wind{
    text-align: center;
    color: rgb(255,102,102);
    font-size: 38px;
}

js

$(document).ready(function(){
    var lat,lon;
    var degree;     //摄氏度
    var tmpShow;    //显示的温度,因为有摄氏与华氏度的转换

    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(function(position){
            lat = position.coords.latitude;
            lon = position.coords.longitude;
            console.log(lat+"  "+lon);

            var para = "https://free-api.heweather.com/v5/weather?city="+lon+","+lat+"&key=53153e2fed574ce19f5c089a1aacede0";
            console.log(para);
            $.getJSON(para,function(json){
                var city= json["HeWeather5"][0]["basic"]["city"];
                var cnty= json["HeWeather5"][0]["basic"]["cnty"];
                var weatherNow = json["HeWeather5"][0]["now"]["cond"]["txt"];
                degree=json["HeWeather5"][0]["now"]["tmp"];
                tmpShow = degree;

                var windShow = json["HeWeather5"][0]["now"]["wind"]["dir"]+" "+json["HeWeather5"][0]["now"]["wind"]["sc"]+"级";
                //console.log(city);
                $("#cityname").html(city+","+cnty);
                $('.weather').html(weatherNow);    
                $(".tmp").html(tmpShow);
                $(".wind").html(windShow);
            });
        });
    }

    $(".btn-tmp-change").on("click",function(){
        console.log($(".tmp").html());
        if(degree == $(".tmp").html()){
            $(".tmp").html(degree*1.8+32);
            $(".tmp-kind").html("°F");
            console.log(degree);
            console.log($(".tmp").html());
        }else{
            $(".tmp").html(degree);
            $(".tmp-kind").html("°C");
        }
    });

});



欢迎大家加入QQ群一起交流讨论, 「吟游」程序人生——YinyouPoet

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 为了在网页底部加入广告代码,您需要编辑网页的HTML代码。在网页的底部添加广告代码,您可以使用以下代码: ``` <div id="bottom-ad"> <!-- 在这里插入您的广告代码 --> </div> ``` 您可以将您的广告代码插入到上面的注释部分。请注意,广告代码应该是HTML格式,并且应该放在<div>标签。 请注意,如果您不熟悉HTML代码的编辑,最好咨询专业的网站开发人员。同时,也要确保您的广告代码符合相关的法规和规定。 ### 回答2: 在网页底部的HTML代码加入相应的广告代码是一种常见的网络广告投放方法。通过将广告代码嵌入到网页底部的HTML,可以使广告显示在网页的底部位置,吸引用户的视线,提高广告的曝光和点击率。 为了实现这个目的,首先需要找到合适的广告代码。广告代码可以从广告主提供的广告平台获取,或者通过与广告网络合作来获取。一旦获得了广告代码,就可以将其嵌入到网页底部的HTML。 嵌入广告代码的具体步骤如下: 1. 打开网页底部的HTML文件或编辑器,可以使用任何文本编辑器软件来编辑HTML文件。 2. 在HTML文件找到网页底部所对应的HTML元素,通常是一个div容器。 3. 在该HTML元素的关闭标签之前,插入广告代码。广告代码可以是一段JavaScript代码或者嵌入式广告标签。 4. 保存并更新网页底部的HTML文件。 5. 在网页上进行测试,确保广告能够正确显示在网页底部位置。 需要注意的是,在加入广告代码时,应该遵循相关的广告平台或广告网络的规定和要求。此外,还要确保广告代码的插入不会影响网页的布局和加载速度,以免影响用户的浏览体验。 总之,通过将广告代码加入网页底部的HTML,可以有效地进行广告投放,提升广告效果和用户参与度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值