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-fluid">
  <div>
        <h1 class="text-center body-text">
          The Local Weather
        </h1>
      <div class="row ">
          <div class="col-md-2 col-md-offset-5 col-xs-12 text-center">
              <span id="data" class="body-text"> Loading...</span>
              <div class="row">
                <div class="col-xs-12 body-text temp-f">
                  <span id="temp"> </span>
                  <button id="temp-button" class="blueButton hidden">F</button>
                </div>
                <div id="conditions" class="col-xs-12 body-text"></div>
                <div id="cond-img" class="col-xs-12"></div>
            </div>
        </div>
         
      </div>
  </div>
</div>
            
          
!
            
              body {
  font-size: 3em;
  font-family: Ubuntu;
  background-color:black;
  color:black;
}

.body-text {
  color:white;
}

.blueButton {
    color:dodgerblue;
    font-weight: bold;
    background-color:black;
    border:none;
}

.area {
  background-color:rgba(255, 0, 0, 0.4);
}

/* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

/* Hide default HTML checkbox */
.switch input {display:none;}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}


.moon {
  width: 3.6875em;
  height: 3.6875em;
  border-radius: 50%;
  margin-left:2.5em;
  box-shadow: .7em .5em 0 0 white;
}

html { box-sizing: border-box; }
html *,
html *:before,
html *:after { box-sizing: inherit; }

h1 {
  margin-bottom: 1.375em;
  color: #fff;
  font-weight: 100;
  font-size: 2em;
  text-transform: uppercase;
}
p,
a {
  color: rgba(255,255,255,0.3);
  font-size: small;
}
p { margin: 1.375rem 0; }

.icon {
  position: relative;
  display: inline-block;
  width: 12em;
  height: 10em;
  font-size: 0.5em; /* control icon size here */
}



.cloud {
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 50%;
  width: 3.6875em;
  height: 3.6875em;
  margin: -1.84375em;
  background: currentColor;
  border-radius: 50%;
  box-shadow:
    -2.1875em 0.6875em 0 -0.6875em,
    2.0625em 0.9375em 0 -0.9375em,
    0 0 0 0.375em #fff,
    -2.1875em 0.6875em 0 -0.3125em #fff,
    2.0625em 0.9375em 0 -0.5625em #fff;
}
.cloud:after {
  content: '';
  position: absolute;
  bottom: 0;
  left: -0.5em;
  display: block;
  width: 4.5625em;
  height: 1em;
  background: currentColor;
  box-shadow: 0 0.4375em 0 -0.0625em #fff;
}
.cloud:nth-child(2) {
  z-index: 0;
  background: #fff;
  box-shadow:
    -2.1875em 0.6875em 0 -0.6875em #fff,
    2.0625em 0.9375em 0 -0.9375em #fff,
    0 0 0 0.375em #fff,
    -2.1875em 0.6875em 0 -0.3125em #fff,
    2.0625em 0.9375em 0 -0.5625em #fff;
  opacity: 0.3;
  transform: scale(0.5) translate(6em, -3em);
  animation: cloud 4s linear infinite;
}
.cloud:nth-child(2):after { background: #fff; }

.sun {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 2.5em;
  height: 2.5em;
  margin: -1.25em;
  background: currentColor;
  border-radius: 50%;
  box-shadow: 0 0 0 0.375em #fff;
  animation: spin 12s infinite linear;
}
.rays {
  position: absolute;
  top: -2em;
  left: 50%;
  display: block;
  width: 0.375em;
  height: 1.125em;
  margin-left: -0.1875em;
  background: #fff;
  border-radius: 0.25em;
  box-shadow: 0 5.375em #fff;
}
.rays:before,
.rays:after {
  content: '';
  position: absolute;
  top: 0em;
  left: 0em;
  display: block;
  width: 0.375em;
  height: 1.125em;
  transform: rotate(60deg);
  transform-origin: 50% 3.25em;
  background: #fff;
  border-radius: 0.25em;
  box-shadow: 0 5.375em #fff;
}
.rays:before {
  transform: rotate(120deg);
}
.cloud + .sun {
  margin: -2em 1em;
}

.rain,
.lightning,
.snow {
  position: absolute;
  z-index: 2;
  top: 50%;
  left: 50%;
  width: 3.75em;
  height: 3.75em;
  margin: 0.375em 0 0 -2em;
  background: currentColor;
}

.rain:after {
  content: '';
  position: absolute;
  z-index: 2;
  top: 50%;
  left: 50%;
  width: 1.125em;
  height: 1.125em;
  margin: -1em 0 0 -0.25em;
  background: #0cf;
  border-radius: 100% 0 60% 50% / 60% 0 100% 50%;
  box-shadow:
    0.625em 0.875em 0 -0.125em rgba(255,255,255,0.2),
    -0.875em 1.125em 0 -0.125em rgba(255,255,255,0.2),
    -1.375em -0.125em 0 rgba(255,255,255,0.2);
  transform: rotate(-28deg);
  animation: rain 3s linear infinite;
}

.bolt {
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -0.25em 0 0 -0.125em;
  color: #fff;
  opacity: 0.3;
  animation: lightning 2s linear infinite;
}
.bolt:nth-child(2) {
  width: 0.5em;
  height: 0.25em;
  margin: -1.75em 0 0 -1.875em;
  transform: translate(2.5em, 2.25em);
  opacity: 0.2;
  animation: lightning 1.5s linear infinite;
}
.bolt:before,
.bolt:after {
  content: '';
  position: absolute;
  z-index: 2;
  top: 50%;
  left: 50%;
  margin: -1.625em 0 0 -1.0125em;
  border-top: 1.25em solid transparent;
  border-right: 0.75em solid;
  border-bottom: 0.75em solid;
  border-left: 0.5em solid transparent;
  transform: skewX(-10deg);
}
.bolt:after {
  margin: -0.25em 0 0 -0.25em;
  border-top: 0.75em solid;
  border-right: 0.5em solid transparent;
  border-bottom: 1.25em solid transparent;
  border-left: 0.75em solid;
  transform: skewX(-10deg);
}
.bolt:nth-child(2):before {
  margin: -0.75em 0 0 -0.5em;
  border-top: 0.625em solid transparent;
  border-right: 0.375em solid;
  border-bottom: 0.375em solid;
  border-left: 0.25em solid transparent;
}
.bolt:nth-child(2):after {
  margin: -0.125em 0 0 -0.125em;
  border-top: 0.375em solid;
  border-right: 0.25em solid transparent;
  border-bottom: 0.625em solid transparent;
  border-left: 0.375em solid;
}

.flake:before,
.flake:after {
  content: '\2744';
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -1.025em 0 0 -1.0125em;
  color: #fff;
  list-height: 1em;
  opacity: 0.2;
  animation: spin 8s linear infinite reverse;
}
.flake:after {
  margin: 0.125em 0 0 -1em;
  font-size: 1.5em;
  opacity: 0.4;
  animation: spin 14s linear infinite;
}
.flake:nth-child(2):before {
  margin: -0.5em 0 0 0.25em;
  font-size: 1.25em;
  opacity: 0.2;
  animation: spin 10s linear infinite;
}
.flake:nth-child(2):after {
  margin: 0.375em 0 0 0.125em;
  font-size: 2em;
  opacity: 0.4;
  animation: spin 16s linear infinite reverse;
}


/* Animations */ 

@keyframes spin {
  100% { transform: rotate(360deg); }
}

@keyframes cloud {
  0% { opacity: 0; }
  50% { opacity: 0.3; }
  100% {
    opacity: 0;
    transform: scale(0.5) translate(-200%, -3em);
  }
}

@keyframes rain {
  0% {
    background: #0cf;
    box-shadow:
      0.625em 0.875em 0 -0.125em rgba(255,255,255,0.2),
      -0.875em 1.125em 0 -0.125em rgba(255,255,255,0.2),
      -1.375em -0.125em 0 #0cf;
  }
  25% {
    box-shadow:
      0.625em 0.875em 0 -0.125em rgba(255,255,255,0.2),
      -0.875em 1.125em 0 -0.125em #0cf,
      -1.375em -0.125em 0 rgba(255,255,255,0.2);
  }
  50% {
    background: rgba(255,255,255,0.3);
    box-shadow:
      0.625em 0.875em 0 -0.125em #0cf,
      -0.875em 1.125em 0 -0.125em rgba(255,255,255,0.2),
      -1.375em -0.125em 0 rgba(255,255,255,0.2);
  }
  100% {
    box-shadow:
      0.625em 0.875em 0 -0.125em rgba(255,255,255,0.2),
      -0.875em 1.125em 0 -0.125em rgba(255,255,255,0.2),
      -1.375em -0.125em 0 #0cf;
  }
}

@keyframes lightning {
  45% {
    color: #fff;
    background: #fff;
    opacity: 0.2;
  }
  50% {
    color: #0cf;
    background: #0cf;
    opacity: 1;
  }
  55% {
    color: #fff;
    background: #fff;
    opacity: 0.2;
  }
}


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

    if (navigator.geolocation) {
        useCoordsForWeather();
    }
    else {
        useIPforWeather();
    }
    
    $("#temp-button").click(handleTempClick);
});


function handleTempClick () {
    // regex to match current temp and obtain it (stored at tempArr[0])
    var regexTemp = /-?\d{1,3}(.\d)?/g;
    var tempArr = regexTemp.exec($("#temp").text());
  
    var curTempUnit = $(this).text();
    var temp = handleTempChange(tempArr[0], curTempUnit).toFixed(1);
  
    $("#temp").html(temp + '&deg;').fadeIn('slow');
    curTempUnit === 'F' ? $(this).html('C') : $(this).html('F');
}

function handleTempChange (tempStr, curUnit) {
    var newTemp = 0;
    var tempFloat = Number(tempStr);
    if (curUnit === 'F')
        newTemp = (tempFloat - 32) * 5 / 9;
    else if (curUnit === 'C')
        newTemp = (tempFloat * 9 / 5) + 32;
    else 
        console.log("There was an issue reading in the current temperature unit.")
    return newTemp;
}

function useCoordsForWeather () {
    navigator.geolocation.getCurrentPosition(function(position) {
        var googleMapsURL = createGoogleMapsURL(position.coords.latitude, position.coords.longitude);
        var latlong = {
            lat: position.coords.latitude,
            long: position.coords.longitude
        };
        localWeatherHandler(latlong, "coords");
    });
}

function useIPforWeather () {
    // uses an geolocation by IP address API to get a rough estimate of location if location services are turned off.
    $.getJSON("https://freegeoip.net/json/", function(locationData) {
        localWeatherHandler(locationData, "zip");
    });
}

function localWeatherHandler (locationData, mode) {
    try {

        var httpsProxy = "https://crossorigin.me/";
        var weatherMapURL = "https://api.wunderground.com/api/17284cf0a020e6d9/conditions/q/QUERY.json";
        weatherMapURL = httpsProxy + weatherMapURL;

        if (mode === "zip") {
            weatherMapURL = weatherMapURL.replace("QUERY",locationData.zip_code);
            $.getJSON(weatherMapURL, function(weatherData) {
                applyWunderToHTML(weatherData);
            });
        }
        else if (mode === "coords") {
            // formats the url which had QUERY intentially placed to make sure easy substitution
            var latlongString = locationData.lat + "," + locationData.long;
            weatherMapURL = weatherMapURL.replace("QUERY", latlongString);
            
            // main ajax call for getting weather data
            $.getJSON(weatherMapURL, function(weatherData) {
							console.log(weatherData);
              applyWunderToHTML(weatherData);
            });
            
            // test call that does not require ajax data
            //applyWunderToHTML(localWeatherData);
            
        }
        else
            throw new Error ("localWeatherHandler Error");
    } catch (e) {
        console.log(e, mode  + " was given as an improper input");
    }

}

function applyWunderToHTML (wunderData) {
    
    var tempF = wunderData.current_observation.temp_f;
    var curCity = wunderData.current_observation.display_location.city;
    var curState = wunderData.current_observation.display_location.state;
    var curWeather = wunderData.current_observation.weather;
    var curCondImgURL = wunderData.current_observation.icon_url.replace("/k/","/i/");
  
    var condHTML = getCondImg(curCondImgURL);
    
    $("#data").fadeOut('slow', function() {
        $(this).html(curCity + ", " + curState).fadeIn('slow');
    });
    $("#temp").fadeOut('slow', function() {
        $(this).html(tempF + '&deg;').fadeIn('slow');
        $("#temp-button").fadeIn('slow').removeClass("hidden");
    });    
    $("#conditions").fadeOut('slow', function() {
        $(this).html(curWeather).fadeIn('slow');
    });
    $("#cond-img").fadeOut('slow', function() {
        $(this).html(condHTML).fadeIn('slow');
    });
  
}

function createGoogleMapsURL (lat, long) {
    var googleMapsURL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=LAT,LONG&sensor=true";
    var googleLatURL = googleMapsURL.replace("LAT", lat);
    var googleLatLongURL = googleLatURL.replace("LONG", long);

    return googleLatLongURL;
}

function getCondImg(wunderImgURL) {
    // url is used because there are fewer images that could be mapped to than there are possible weather conditions. 
    //https://serbian.wunderground.com/weather/api/d/docs?d=resources/phrase-glossary&MR=1
  
    // html for various weather icons
    var conditions = {
        snow:'<div class="icon flurries"><div class="cloud"></div><div class="snow"><div class="flake"></div><div class="flake"></div></div></div>',
        rain:'<div class="icon rainy"><div class="cloud"></div><div class="rain"></div></div>',
        storms:'<div class="icon thunder-storm"><div class="cloud"></div><div class="lightning"><div class="bolt"></div><div class="bolt"></div></div></div>',
        clearDay:'<div class="icon sunny"><div class="sun"><div class="rays"></div></div></div>',
        clearNight:'<div class="moon"></div>',
        cloudy:'<div class="icon cloudy"><div class="cloud"></div><div class="cloud"></div></div>'
    };
  
    function isNightTime (hour) {
        // currentHour is less than 5am or greater than 6pm
        return (hour < 6 || hour > 18);
    }
    
    // parse image url using regex for different conditions
    var regexConditions = [/(snow|sleet|flurries)/, /(rain)/, /(storm)/, /(fog|hazy|cloud)/, /(clear|sunny)/]
    var matchIndex = 0;
        
    // figures out which condition matches the url
    regexConditions.forEach(function(element, index, array) {
        if (element.test(wunderImgURL)) {
            matchIndex = index;
        }
    });

    // accounts for a clear night condition
    if (matchIndex === 4 && isNightTime(new Date().getHours()))
        matchIndex = 5;
                                             
    
  
    switch(matchIndex) {
      case 0: return conditions.snow;
      case 1: return conditions.rain;
      case 2: return conditions.storms;
      case 3: return conditions.cloudy;
      case 4: return conditions.clearDay;
      case 5: return conditions.clearNight;
      
      default: 
        console.log("An error occurred in the switch statement.");
        return conditions.cloudy;
    }
}


///////////////
// Test Data //
///////////////
// Move to the top to no longer need ajax calls to get data

//localWeatherData = {"response":{"version":"0.1","termsofService":"http://www.wunderground.com/weather/api/d/terms.html","features":{"conditions":1}},"current_observation":{"image":{"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png","title":"Weather Underground","link":"http://www.wunderground.com"},"display_location":{"full":"Holyoke, MA","city":"Holyoke","state":"MA","state_name":"Massachusetts","country":"US","country_iso3166":"US","zip":"01075","magic":"1","wmo":"99999","latitude":"42.260000","longitude":"-72.650000","elevation":"63.00000000"},"observation_location":{"full":"New City, Easthampton, Massachusetts","city":"New City, Easthampton","state":"Massachusetts","country":"US","country_iso3166":"US","latitude":"42.273300","longitude":"-72.655289","elevation":"180 ft"},"estimated":{},"station_id":"KMAEASTH22","observation_time":"Last Updated on August 11, 11:34 PM EDT","observation_time_rfc822":"Thu, 11 Aug 2016 23:34:45 -0400","observation_epoch":"1470972885","local_time_rfc822":"Thu, 11 Aug 2016 23:34:45 -0400","local_epoch":"1470972885","local_tz_short":"EDT","local_tz_long":"America/New_York","local_tz_offset":"-0400","weather":"Clear","temperature_string":"77.9 F (25.5 C)","temp_f":77.9,"temp_c":25.5,"relative_humidity":"87%","wind_string":"Calm","wind_dir":"South","wind_degrees":180,"wind_mph":0,"wind_gust_mph":0,"wind_kph":0,"wind_gust_kph":0,"pressure_mb":"1015","pressure_in":"29.97","pressure_trend":"-","dewpoint_string":"74 F (23 C)","dewpoint_f":74,"dewpoint_c":23,"heat_index_string":"NA","heat_index_f":"NA","heat_index_c":"NA","windchill_string":"NA","windchill_f":"NA","windchill_c":"NA","feelslike_string":"77.9 F (27 C)","feelslike_f":"77.9","feelslike_c":"27","visibility_mi":"10.0","visibility_km":"16.1","solarradiation":"--","UV":"0","precip_1hr_string":"-999.00 in ( 0 mm)","precip_1hr_in":"-999.00","precip_1hr_metric":" 0","precip_today_string":"0.00 in (0 mm)","precip_today_in":"0.00","precip_today_metric":"0","icon":"clear","icon_url":"http://icons.wxug.com/i/c/k/clear.gif","forecast_url":"http://www.wunderground.com/US/MA/South_Hadley.html","history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KMAEASTH22","ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=42.273300,-72.655289","nowcast":""}};

            
          
!
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