以下代码段未显示任何内容。这是我codepen的代码。你能帮我弄清楚什么是错的,怎么解决?我也检查了,API链接是正确的。
$(document).ready(function() {
$(".cityarea").html(getLocation);
testMenu();
});
var currentLat;
var currentLong;
var currentCity;
var currentRegion;
var currentCountry;
var mainCities = {
'San_Francisco': {
'region': 'California',
'country': "United States",
'lat': 37.7749300,
'lon': -122.4194200
},
'St._Louis': {
'region': 'Missouri',
'country': "United States",
'lat': 38.6272700,
'lon': -90.1978900
},
'Miami': {
'region': 'Florida',
'country': "United States",
'lat': 25.7742700,
'lon': -80.1936600
},
'Tokyo': {
'region': 'Tokyo',
'country': "Japan",
'lat': 35.689500,
'lon': 139.6917100
},
'Denver': {
'region': 'Colorado',
'country': "United States",
'lat': 39.739100,
'lon': -104.984700
}
};
function testMenu() {
for (var place in mainCities) {
var city = place.replace(/_/g, ' ');
$('#testMenu').append("<li onclick=testWeather('" + place + "');><a href='#'>" + city + "</a></li>");
}
};
function testWeather(cityLocation) {
currentLat = mainCities[cityLocation].lat;
currentLong = mainCities[cityLocation].lon;
currentRegion = mainCities[cityLocation].region;
currentCity =cityLocation.replace(/_/g, ' ');
currentCountry = mainCities[cityLocation].country;
getWeather();
};
function getLocation() {
$.getJSON('http://ip-api.com/json', function(data) {
currentRegion = data.regionName;
//currentCity = data.city;
currentCountry = data.country;
currentLat = data.lat;
currentLong = data.lon;
getWeather();
});
};
function getWeather() {
$("#state").text(currentRegion);
$("#country").text(currentCountry);
$.getJSON('http://api.openweathermap.org/data/2.5/weather?lat=' + currentLat + '&lon=' + currentLong + '&units=imperial&APPID=e656b9ee098cf2341fcfdb365b96b4a8', function(json) {
$("#cityname").text(json.name);
var showfahrenheit = true;
var tempfahrenheit = Math.round(json.main.temp);
var temcelcius = Math.round((tempfahrenheit - 32) * 5/9);
$("#temp").html(tempfahrenheit);
$('#unit-switch').on('click', function() {
if (showfahrenheit === false) {
$("#temp").html(tempfahrenheit);
showfahrenheit = true;
} else {
$("#temp").html(temcelcius);
showfahrenheit = false;
}
$("#unit-toggle").toggleClass("toggle");
});
var prefix = "wi wi-owm-";
var weatherIcons = json.weather[0].id;
var icon = prefix + weatherIcons;
$("#wparameter").html("<i class='" + icon + "'></i>"); $("#wdescription").text(json.weather[0].description);
var weatherImages = {
'rain': 'https://s6.postimg.org/7ug4yocip/rainpic1.jpg', 'clear': 'https://s6.postimg.org/alzepab1d/clearskypic1.jpg',
'snow': 'https://s6.postimg.org/rdkq81ba9/snowpic.jpg',
'clouds': 'https://s6.postimg.org/4v4mxtdnl/cloudspic.jpg',
'fog': 'https://s6.postimg.org/r9mbe1gf5/fogpic.jpg',
'thunder': 'https://s6.postimg.org/qpbvp3ckh/thunderstormpic.jpg',
'windy': 'https://s6.postimg.org/wg222tkkh/windybreezepic.jpg',
'dust': 'https://s6.postimg.org/tpo4rvyht/dustsandpic.jpg',
'tornadostorm': 'https://s6.postimg.org/aamjq9jz5/tornadostorm.jpg'
};
var imagepic;
if (weatherIcons >= 200 && weatherIcons <= 232 ) {
imagepic = weatherImages.thunder;
} else if (weatherIcons >= 300 && weatherIcons <= 531) {
imagepic = weatherImages.rain;
} else if (weatherIcons >= 600 && weatherIcons <= 622) {
imagepic = weatherImages.snow;
} else if (weatherIcons >= 701 && weatherIcons <= 731) {
imagepic = weatherImages.dust;
} else if (weatherIcons == 741) {
imagepic = weatherImages.fog;
} else if (weatherIcons >= 751 && weatherIcons <= 781) {
imagepic = weatherImages.dust;
} else if (weatherIcons == 800) {
imagepic = weatherImages.clear;
} else if (weatherIcons >= 801 && weatherIcons <= 804) {
imagepic = weatherImages.clouds;
} else if (weatherIcons >= 900 && weatherIcons <= 906) {
imagepic = weatherImages.tornadostorm;
} else if (weatherIcons >= 951 && weatherIcons <= 962) {
imagepic = weatherImages.windy;
} else {}
var speedimg = 500;
$(".wrapper").animate({opacity: 0}, speedimg, function() {
$(".wrapper").animate({opacity: 1}, speedimg);
$(".wrapper").css(
'background-image', 'url(' + imagepic + ')');
});
});
};
@import url('https://fonts.googleapis.com/css?family=Roboto:300,400');
body {
position: relative;
}
html, body{
height:100%;
margin: 0;
}
.wrapper {
width: 100%;
height: 100%;
position: relative;
display:flex;
flex-direction:column;
justify-content:center;
align-items:center;
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
}
.container {
position: relative;
display: block;
margin: 0 auto;
width: 35%;
min-width: 300px;
background-color: rgba(255, 255, 255, 0.9);
border-radius: 3px;
border: 2px solid #009688;
/*padding: 10px 20px;*/
box-shadow: 0 8px 10px 1px rgba(0,0,0,.14), 0 3px 14px 2px rgba(0,0,0,.12), 0 5px 5px -3px rgba(0,0,0,.2);
}
.header h1 {
text-align: center;
font-family: 'Roboto', sans-serif;
font-weight: 300;
margin: 0 0 10px 0;
color: #000000;
}
.weatherbox {
text-align: center;
}
.locationbox {
position: relative;
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
-webkit-justify-content: space-between;
justify-content: space-between;
padding: 10px 20px 0px;
}
.locactioninfobox {
/* -webkit-flex-basis: 350px;
flex-basis: 350px;*/
text-align: left;
}
.cityarea h2 {
color: #000000;
font-family: 'Roboto', sans-serif;
font-weight: normal;
font-size: 2em;
margin: 0;
}
.countryarea {
position: relative;
display: -webkit-flex;
display: flex;
/*-webkit-justify-content: center;
justify-content: center;*/
margin: 0 auto;
text-align: left;
}
.countryarea h3 {
margin: 0 0 10px 0;
font-family: 'Roboto', sans-serif;
font-weight: normal;
color: #000000;
}
.countryarea h3:first-child {
margin-right: 8px;
}
.dropdownlocation {
-webkit-align-self: center;
align-self: center;
}
.dropdown {
position: relative;
/*display: inline-block;*/
font-size: 16px;
color: #FFF;
}
.dropdown-header {
display: block;
padding: 3px 20px;
font-size: 12px;
line-height: 1.42857143;
color: #777;
white-space: nowrap;
text-transform: uppercase;
}
.dropdown > span {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.dropdown span {
box-sizing: border-box;
/*display: inline-block;*/
width: 100%;
background-color: #57A0D4;
padding: 10px 20px;
cursor: pointer;
text-align: center;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
border-radius: 5px;
font-size: 20px;
}
.dropdown span .fa {
display: inline;
}
.dropdown span .fa-globe {
margin-right: 10px;
}
.dropdown > span,
.dropdown > div {
cursor: pointer;
outline: 0;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
.dropdown > div {
background-color: rgba(0, 0, 0, 0);
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: none;
}
.dropdown > span:focus ~ div {
display: block;
}
.dropdown > ul {
position: absolute;
list-style: none;
text-align: left;
/*width: 100%;*/
min-width: 160px;
z-index: 1;
visibility: hidden;
transition: visibility 0.5s;
opacity: 0;
padding: 5px 0;
margin: 2px 0 0;
font-size: 14px;
text-align: left;
box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.2);
/*display: none;*/
background-color: #fff;
border: 1px solid #ccc;
border: 1px solid rgba(0,0,0,.15);
border-radius: 4px;
-webkit-box-shadow: 0 6px 12px rgba(0,0,0,.175);
box-shadow: 0 6px 12px rgba(0,0,0,.175);
}
.dropdown > span:focus ~ ul {
visibility: visible;
opacity: 1;
}
ul li{
/*padding: 15px;*/
background-color: #fff;
color: #4FB9A7;
margin-bottom: 1px;
cursor: pointer;
}
ul li a {
padding: 8px 20px;
color: inherit;
text-decoration: none;
display: block;
}
ul li a:hover{
background-color: #4FB9A7;
color: #FFF;
}
ul .divider {
height: 1px;
margin: 9px 0;
overflow: hidden;
background-color: #e5e5e5;
}
.mainline hr {
border: 0;
height: 1px;
background: #000000;
opacity: 0.2;
}
.temperaturearea {
padding: 0px 20px 10px;
}
.toptempbox {
position: relative;
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
}
.tempbox {
-webkit-flex-basis: 180px;
flex-basis: 180px;
}
.temperaturearea span#temp {
position: relative;
color: #000000;
font-family: 'Roboto', sans-serif;
font-size: 10em;
}
.temperaturearea #temp:after {
content: '';
position: absolute;
height: 10px;
width: 10px;
top: 16px;
right: -17px;
border: 3px solid #000000;
border-radius: 50%;
}
.wconditionsbox {
-webkit-align-self: flex-end;
align-self: flex-end;
padding-bottom: 35px;
padding-left: 5px;
-webkit-flex-basis: 100px;
flex-basis: 100px;
}
.wconditionsbox span {
display: block;
text-align: left;
}
#wparameter {
font-size: 3.5em;
}
#wdescription {
font-size: 1em;
}
#wparameter, #wdescription {
color: #000000;
font-family: 'Roboto', sans-serif;
}
.weather > span {
position: relative;
font-family: 'Roboto', sans-serif;
font-size: 1.2rem;
color: #000000;
}
.weather > span:before {
content: '';
position: absolute;
left: -10px;
top: 0px;
height: 3px;
width: 3px;
border: 2px solid #000000;
border-radius: 50%;
}
.main-toggle span {
margin: 0 0 0 16px;
}
.main-toggle span:last-child {
margin-left: 11px;
}
.weather button {
background: #6bbf6b;
border: none;
border-radius: 30px;
outline: none;
width: 45px;
height: 20px;
margin: 5px 5px 0;
cursor: pointer;
position: relative;
transition: background .2s;
}
.weather button:active {
background: #67b567;
}
.weather #unit-toggle {
position: absolute;
display: inline-block;
left: -8px;
top: 2px;
width: 15px;
height: 15px;
background: #fff;
border-radius: 50%;
transition: left .2s;
}
#unit-toggle.toggle {
left: 16px;
}
@media screen and (max-width: 479px) {
.container {
width: 85%;
min-width: 300px;
}
}
@media screen and (min-width: 480px) and (max-width: 900px) {
.container {
width: 65%;
min-width: 300px;
}
}
<html lang="en">
<head>
<meta charset="utf-8">
<title>Local Weather</title>
</head>
<body class="full">
<div class="wrapper">
<div class="container">
<div class="header"><h1></h1></div>
<div class="weatherbox">
<div class="locationbox">
<div class="locactioninfobox">
<div class="cityarea">
<h2 id="cityname"></h2>
</div>
<div class="countryarea">
<h3 id="state"></h3>
<h3 id="country"></h3>
</div>
</div>
<div class="dropdownlocation">
<div class="dropdown">
<span tabindex="0" onclick="return true"><i class="fa fa-globe" aria-hidden="true"></i><i class="fa fa-caret-down" aria-hidden="true"></i></span>
<div tabindex="0" onclick="return true"></div>
<ul id="testMenu">
<li onclick="getLocation();"><a href="#">Current Location</a></li>
<li class="divider"></li>
<li class="dropdown-header">Main Cities</li>
</ul>
</div>
</div>
</div>
<div class="mainline"><hr></div>
<div class="temperaturearea">
<div class="toptempbox">
<div class="tempbox">
<span id="temp"></span>
</div>
<div class="wconditionsbox">
<span id="wparameter"></span>
<span id="wdescription"></span>
</div>
</div>
<div class="weather main-toggle"> <!-- Begin Unit Switch -->
<span>F</span>
<button id="unit-switch"><span id="unit-toggle"></span></button>
<span>C</span>
</div>
</div>
</div>
</div>
</div>
</body>
</html>