JSFiddle - React, Tailwind, and code Playground
by laurelrose18
HTML
<body>
<header>
<h1>Weather Forecast</h1>
</header>
<div id="weather">
<ul id="scroller">
<!-- The forecast items will go here -->
</ul>
<a href="#" class="arrow previous">Previous</a>
<a href="#" class="arrow next">Next</a>
</div>
<p class="location"></p>
<div id="clouds"></div>
<!-- JavaScript includes - jQuery, moment.js and our own script.js -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.0.0/moment.min.js"></script>
<script src="assets/js/script.js"></script>
</body>
CSS
/*-------------------------
Simple reset
--------------------------*/
*{
margin:0;
padding:0;
}
/*-------------------------
General Styles
--------------------------*/
html{
background:url('../img/bg.jpg') repeat #e1f0f6;
}
body{
font:14px/1.3 'Open Sans Condensed', sans-serif;
}
a, a:visited {
outline:none;
color:#1c4f64;
}
a:hover{
text-decoration:none;
}
section, footer, header{
display: block;
}
/*----------------------------
The Header
-----------------------------*/
header h1{
text-align:center;
font: normal 52px 'Playball', cursive;
color:#518191;
padding-top:60px;
text-shadow:1px 1px 1px rgba(0,0,0,0.1);
}
/*----------------------------
The Cloud Background
-----------------------------*/
#clouds{
background:url('../img/cloud1.png') no-repeat;
height: 145px;
left: 50%;
position: absolute;
top: 250px;
margin-left: -542px;
width: 222px;
z-index: -1;
}
#clouds:before{
content:'';
background:url('../img/cloud2.png') no-repeat;
height: 102px;
right: -710px;
position: absolute;
top: -125px;
width: 156px;
}
#clouds:after{
content:'';
background:url('../img/cloud3.png') no-repeat;
height: 74px;
right: -750px;
position: absolute;
top: 325px;
width: 114px;
}
/*----------------------------
The Weather Div
-----------------------------*/
#weather{
position:relative;
width:494px;
height:407px;
margin: 50px auto 25px;
overflow:hidden;
}
#weather:before{
background: url("../img/preloader.gif") no-repeat center center #111;
border: 1px solid #000;
border-radius: 50% 50% 50% 50%;
box-shadow: 0 1px 2px #444;
content: "";
height: 44px;
left: 50%;
margin: -22px 0 0 -22px;
opacity: 0.7;
position: absolute;
top: 50%;
width: 44px;
}
#weather.error:before{
display:none;
}
#weather.error{
text-align:center;
font-size: 20px;
}
#weather.loaded #scroller,
#weather.loaded...
JavaScript
$(function(){
/* Configuration */
var DEG = 'c'; // c for celsius, f for fahrenheit
var weatherDiv = $('#weather'),
scroller = $('#scroller'),
location = $('p.location');
// Does this browser support geolocation?
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(locationSuccess, locationError);
}
else{
showError("Your browser does not support Geolocation!");
}
// Get user's location, and use OpenWeatherMap
// to get the location name and weather forecast
function locationSuccess(position) {
try{
// Retrive the cache
var cache = localStorage.weatherCache && JSON.parse(localStorage.weatherCache);
var d = new Date();
// If the cache is newer than 30 minutes, use the cache
if(cache && cache.timestamp && cache.timestamp > d.getTime() - 30*60*1000){
// Get the offset from UTC (turn the offset minutes into ms)
var offset = d.getTimezoneOffset()*60*1000;
var city = cache.data.city.name;
var country = cache.data.city.country;
$.each(cache.data.list, function(){
// "this" holds a forecast object
// Get the local time of this forecast (the api returns it in utc)
var localTime = new Date(this.dt*1000 - offset);
addWeather(
this.weather[0].icon,
moment(localTime).calendar(), // We are using the moment.js library to format the date
this.weather[0].main + ' <b>' + convertTemperature(this.main.temp_min) + '°' + DEG +
' / ' + convertTemperature(this.main.temp_max) + '°' + DEG+'</b>'
);
});
// Add the location to the page
location.html('<b>'+'The '+ ' astrocast' +' for '+city);
weatherDiv.addClass('loaded');
// Set the slider to the first slide
showSlide(0);
}
else{
// If the cache is old or nonexistent, issue a new AJAX request
var weatherAPI =...