Weather Widget
by oterox
HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/weather-icons/1.3.2/css/weather-icons.min.css">
<div class="component__weather-box">
<div class="component__weather-content">
<div class="weather-content__overview"></div>
<div class="weather-content__temp"></div>
</div>
<div class="component__forecast-box"></div>
</div>
CSS
/* Styles */
body {
background: #dce2e5;
font-family: "Roboto", sans-serif;
font-weight: 100;
font-size: 11px;
}
.component__weather-box {
margin: 30px auto;
width: 420px;
border: 1px solid #b7b178;
border-radius: 10px;
overflow: hidden;
box-shadow: 2px 5px 20px 1px #444;
}
.component__weather-content {
position: relative;
overflow: hidden;
color: #fff;
background: #b7b178;
height: 120px;
}
.component__weather-content:before {
content: "";
display: block;
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 1;
opacity: 0.3;
background-repeat: no-repeat;
background-position: 0 63%;
background-size: cover;
}
.weather-content__overview {
width: 50%;
text-align: center;
display: inline-block;
float: left;
z-index: 2;
position: relative;
}
h1 {
font-weight: 400;
font-size: 40px;
line-height: 40px;
padding-bottom: 0;
margin-bottom: 0;
margin-top: 0.75em;
}
.weather-content__temp {
width: 50%;
z-index: 2;
text-align: center;
float: left;
font-size: 50px;
text-align: center;
margin-top: 0.5em;
position: relative;
vertical-align: middle;
}
.weather-content__temp .degrees {
line-height: 40px;
}
.weather-content__temp .wi-degrees {
margin-left: -10px;
vertical-align: top !important;
}
.currentTemp .wi {
margin-right: 20px;
font-size: 40px;
vertical-align: baseline;
}
.component__forecast-box {
display: flex;
clear: both;
}
.forecast__item {
flex: 1;
text-align: center;
}
.forecast-item__heading {
background: #b7b178;
border: 1px solid #ccc;
border-left: none;
text-transform: uppercase;
color: #fff;
font-weight: 800;
padding: 10px;
}
.forecast-item__info {
background: #fff;
color: #b7b178;
padding-bottom: 10px;
border-right: 1px solid #d64826;
}
.forecast-item__info .wi {
display: block;
margin: 0 auto;
font-size: 24px;
padding: 15px 0;
}
.forecast-item__info .degrees {
font-size: 20px;
...
JavaScript
const CURRENT_LOCATION = document.getElementsByClassName('weather-content__overview')[0];
const CURRENT_TEMP = document.getElementsByClassName('weather-content__temp')[0];
const FORECAST = document.getElementsByClassName('component__forecast-box')[0];
// Use Fetch API to GET data from OpenWeather API
// return json
function getWeatherData(position) {
let headers = new Headers();
const URL = `https://api.openweathermap.org/data/2.5/forecast/daily?q=alcobendas,es&cnt=7&units=metric&APPID=e43f64ee98be9268f7a7f49e34aecfdf`;
return fetch(URL, {
method: 'GET',
headers: headers
}).then(data => data.json());
}
// TUTORIAL READERS:
// yeah, using an external resource for the icons and applying them here using a switch block
function applyIcon(icon) {
let selectedIcon;
switch (icon) {
case '01d':
selectedIcon = "wi-day-sunny"
break;
case '01n':
selectedIcon = "wi-night-clear"
break;
case '02d':
case '02n':
selectedIcon = "wi-cloudy"
break;
case '03d':
case '03n':
case '04d':
case '04n':
selectedIcon = "wi-night-cloudy"
break;
case '09d':
case '09n':
selectedIcon = "wi-showers"
break;
case '10d':
case '10n':
selectedIcon = "wi-rain"
break;
case '11d':
case '11n':
selectedIcon = "wi-thunderstorm"
break;
case '13d':
case '13n':
selectedIcon = "wi-snow"
break;
case '50d':
case '50n':
selectedIcon = "wi-fog"
break;
default:
selectedIcon = "wi-meteor"
}
return selectedIcon;
}
// Use returned json from promise to render daily forecast
renderData = (location, forecast) => {
// render city, current weather description and temp
const currentWeather = forecast[0].weather[0];
const widgetHeader = `<h1>${location.name}</h1><small>${currentWeather.description}</small>`;
CURRENT_TEMP.innerHTML = `<i class="wi ${applyIcon(currentWeather.icon)}"></i>...