123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container">
	<div class="weather_card">
		<div class="row">
			<div class="large-12 columns">
				<h1 class="text-center"><strong>Local Weather</strong></h1>
				<h3 id="position" class="text-center"></h3>
			</div>
		</div>
		<div class="row text-center">
			<div class="large-6 columns">
				<h3>Current Weather</h3>
				<h1><i id="icon"></i></h1>
				<h4 id='current_w'></h4>
				<h4 id='current_temp'></h4>
			</div>
			<div class="large-6 columns">
				<h3>Daily Forecast</h3>
				<ul id="details">

				</ul>
			</div>
		</div>
	</div>
</div>
<div class="large-12 columns text-center sticky">
	<p> dynamic background images provided by <a href="https://source.unsplash.com" targe="_blank">source.unsplash</a> </p>
	<p> weather icons care of <a href="https://weathericons.io" target="_blank">weathericons.io</a>
</div>
            
          
!
            
              body {
	background-image: url('https://source.unsplash.com/featured/?weather');
	background-repeat: no-repeat;
	background-size: cover;
	color: #fff;
}

.container {
	//margin: 50px 0px;
	padding: 50px 0px;
	min-height: 100%;
}

.weather_card {
	background:rgba(255,255,255,0.7);
	color: #000;
	border-radius: 10px;
	min-width:400px;
	max-width:800px;
	margin: auto;
	padding:20px 40px 40px 20px;
}

.high {
	color: blue;
}

.low {
	color: red;
}

#details li {
	list-style: none;
}

            
          
!
            
              $(document).ready( function(){

    var lat;
    var lon;
    var city;
    var zip;
    var currentWeather;
    var currentTemp;
		var sunrise;
		var sunset;
    var icon;
    var unsplash;
    var daily;
    var minTemp;
    var maxTemp;
    var currentAPI;
    var forecastAPI;
    var ow_api = "85d3be3684ef7f65d6f54b6fe32ac79d";
    var locationAPI = "https://geoip-db.com/json/geoip.php?jsonp=?";

    // Get Location Information

    $.getJSON(locationAPI)
			.done (function(location){
        lat = location.latitude;
        lon = location.longitude;
        city = location.city;
        zip = location.postal;
        currentAPI = "http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&appid=" + ow_api;
        forecastAPI = "http://api.openweathermap.org/data/2.5/forecast/daily/?lat=" + lat + "&lon=" + lon + "&cnt=1&appid=" + ow_api;
    });

    // openweathermap API calls

    // Current Weather

    $.getJSON(currentAPI, function(current){
        currentWeather = current.weather[0].description;
        currentTemp = convertTemp(current.main.temp,"f");
				sunrise = current.sys.sunrise;
				sunset = current.sys.sunset;
        unsplash = "url('https://source.unsplash.com/featured/?" + current.weather[0].main + "')";
        icon = getIcon(current.weather.weather[0].id, sunrise, sunset);
    })

		// Daily Forecast

		$.getJSON(forecastAPI, function(daily){
			daily = daily.list[0].weather[0].description;
			minTemp = daily.list[0].temp.min;
			maxTemp = daily.list[0].temp.max;
		})

	// Update Page with Information
	
	$("#position").html(city + ", " + zip);
	$("#icon").html(icon);
	

	//helpers
	function convertTemp(kelvin,arg) {
			if (arg == "f") {
					return Math.round((kelvin*(9/5))-459.67);
			} else {
					return Math.round(kelvin - 273.15);
			}
	}

	function titleCase(str) {
		var strArray = str.split(" ");
		var output = new Array;
		for (var i = 0; i < strArray.length; i++){
		output.push( strArray[i].replace( /\w'?\S*/, function(word){
			return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
			}));

		}
		return output.join(" ");
	}

	function getIcon(main,sunrise,sunset) {
		var now = Math.round(new Date().getTime()/1000);
		var icon = "wi wi-";

		icon += (now > sunrise && now < sunset ) ? "day-" : "night-";

		switch(true){
			case (main < 300):
				icon += "thunderstorm"; //thunderstorm
				break;
			case (main < 400):
				icon += "showers"; //drizzle
				break;
			case (main < 600):
				icon += "rain"; //rain
				break;
			case (main < 700):
				icon += "snow"; // snow
				break;
			case (main < 800):
				icon = "wi-smoke"; //fog/haze/sandstorm/ash
				break;
			case (main == 800):
				icon += (now > sunrise && now < sunset ) ? "sunny" : "night-clear";
				break;
			case (main < 805):
				icon += "cloudy"; //clouds
				break;
			case (main < 903):
				icon = "wi-hurricane"; //cyclone
				break;
			case (main < 905):
				icon = "wi-thermometer"; //temperature
				break;
			case (main == 905):
				icon = "wi-strong-wind"; //windy
				break;
			case (main == 906):
				icon = "wi-hail"; //hail
				break;
			case (main < 957):
				icon += "cloudy-gusts"; //breeze
				break;
			case (main <= 963 ):
				icon += "storm-showers"; // storm
				break;
			default:
				icon = "wi-alien";
			}
		return icon;
	}
	

});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console