<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>现在天气</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet">
<link rel="stylesheet" href="./css/style.css">
</head>
<body>
<!-- https://s1.ax1x.com/2018/10/10/iY4xvq.jpg -->
<div class="container-fluid">
<div class="content">
<div class="city">
<div id="cityname">城市</div>
</div>
<div class="weather">天气</div>
<div class="degree">
<div class="tmp">30</div>
<div class="tem-kind">°C</div>
</div>
<div class="wind">北风3级</div>
</div>
</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<script src="./js/index.js"></script>
</html>
*{
margin: 0;
padding: 0;
}
body{
overflow: none;
background: url("https://s1.ax1x.com/2018/10/10/iY4xvq.jpg");
background-size: cover;
}
.content{
position: absolute;
margin-top: 10%;
margin-left: 20%;
margin-right: 20%;
width: 60%;
}
.city{
text-align: center;
}
.city>div{
font-size: 28px;
vertical-align: middle;
color: #2E94B9;
}
.weather{
text-align: center;
color: #2E94B9;
font-size: 38px;
}
.degree{
text-align: center;
color: #2E94B9;
font-size: 38px;
}
.degree div{
display: inline;
}
.degree .tmp-kind{
margin-left: 10px;
}
.degree .tmp-change{
margin-left: 30px;
}
.wind{
text-align: center;
color: #2E94B9;
font-size: 38px;
}
$(document).ready(function(){
var lat , lon;
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/s6/weather?location="+lon+","+lat+"&key=e9b54fa5486c4d709f80c608d2a170d1";
console.log(para);
$.getJSON(para , function (json) {
var city= json["HeWeather6"][0]["basic"]["parent_city"];
var area= json["HeWeather6"][0]["basic"]["admin_area"];
var weatherNow = json["HeWeather6"][0]["now"]["cond_txt"];
var degree = json["HeWeather6"][0]["now"]["tmp"];
var windShow = json["HeWeather6"][0]["now"]["wind_dir"] + " " + json["HeWeather6"][0]["now"]["wind_spd"] + "级";
console.log(windShow);
$("#cityname").html(city+","+area);
$('.weather').html(weatherNow);
$(".tmp").html(degree);
$(".wind").html(windShow);
});
})
}
});