Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="bg"></div>
<div class="Weather column column-center">
  <div class="Weather__current">
    <h1 class="title">Current Weather</h1>
    <div class="Weather__current-wrap column column-center">
      <h1 class="Weather__current-location" id="location">Your Location</h1>
      <h1 class="Weather__current-temp" id="temp">70 &deg;F</h1>
      <p class="Weather__current-condition" id="condition">Sunny</p>
      <button id="toggle">Show Celcius</button>
    </div>
  </div>
  <div class="Weather__forecast column column-center">
    <h1 class="title">5 Day Forecast</h1>
    <div class="Weather__forecast-wrap row"></div>
  </div>
</div>
<div class="range column column-center">
  <h2>Test Buttons:</h2>
  <div class="row">
    <button id="cold">Below 40</button>
    <button id="cool">Below 55/Above 40</button>
    <button id="warm">Below 70/Above 55</button>
    <button id="perfect">Below 85/Above 69</button>
    <button id="hot">Above 85</button>
  </div>
  <div class="row">
    <button id="reset">Reset To Current Temp</button>
  </div>
</div>

              
            
!

CSS

              
                // Resets
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Lato', sans-serif;
}

button {
  outline: none;
  background: none;
  cursor: pointer;
}

// Globals
.row, .column {
  display: flex;
}

.column {
  flex-direction: column;
}

.center {
  justify-content: center;
}

.column-center {
  align-items: center;
}

.split {
  justify-content: space-between;
}

// Page Styles 
.bg {
  background-size: cover;
  background-repeat: no-repeat;
  min-height: 100vh;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
  opacity: .3;
}

.Weather {
  padding-top: 50px;
  .title {
    font-weight: 300;
    margin-bottom: 20px;
  }
  &__current {
    margin-bottom: 40px;
    &-location {
      font-weight: 300;
      margin-bottom: 10px;
      
    }
    &-temp {
      font-size: 3rem;
      font-weight: 400;
      color: #555;
    }
    &-condition {
      font-size: 1.3rem;
      letter-spacing: 1px;
      margin-top: 5px;
      color: #555;
    }
    #toggle {
      border: 1px solid #333;
      color: #333;
      padding: 3px 10px;
      border-radius: 2px;
      margin-top: 10px;
      &:active {
        background: rgba(0, 0, 0, .1);
      }
    }
  }
  &__forecast {
    &-day {
      margin: 0 15px;
      h2 {
        font-weight: 300;
        font-size: 1.4rem;
        margin-bottom: 5px;
      }
      .wi {
        font-size: 2rem;
        color: #5f5f5f;
        margin-bottom: 8px;
      }
    }
    &-high, &-low {
      font-size: 1.05rem;
    }
    &-high {
      color: #333;
      margin-right: 10px;
    }
    &-low {
      color: #6e6e6e;
    }
  }
}

.range {
  margin-top: 20px;
  h2 {
    margin-bottom: 12px;
    font-weight: 300;
    position: relative;
  }
  button {
    border: 1px solid #444;
    padding: 3px 5px;
    border-radius: 2px;
    margin: 0 5px;
    &:active {
      background: rgba(0, 0, 0, .1);
    }
  }
  #reset {
  border: 1px solid #444;
  padding: 3px 5px;
  border-radius: 2px;
  margin-top: 15px;
}
}


              
            
!

JS

              
                var weather = (function() {
  var lat, long, country, city, tempF, tempC, condition, forecast, 
      isFar = true, lows = [], highs = [];
  
  var init = function() {
    getLocation();
    $('#toggle').on('click', toggleUnit);
    $('.range').find('button').on('click', changeBg);
    $('#reset').on('click', reset);
  };
  
  var getLocation = function() {
    if ( window.chrome ) {
      $.getJSON('http://ip-api.com/json', function(json) {
        lat = json.lat;
        long = json.lon;
        getWeather();
      });
    } else {
      if ( navigator.geolocation ) {
        navigator.geolocation.getCurrentPosition(function(data) {
          lat = data.coords.latitude;
          long = data.coords.longitude;
          getWeather();
        });
      }
    }
  };
  
  var getWeather = function() {
    
    // Get Current Weather
    $.getJSON('http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + long + '&units=imperial&APPID=3acc16ffae9e45df92a064e41646355f', function(json) {
      city = json.name;
      country = json.sys.country;
      tempF = Math.round(json.main.temp);
      tempC = Math.round((tempF - 32) * 5 / 9);
      condition = json.weather[0].description.split(' ').map(function(a) {
        return a[0].toUpperCase() + a.slice(1);
      }).join(' ');
      showCurrentWeather();
    });
    
    // Get Forecast Weather  
    $.getJSON('http://api.openweathermap.org/data/2.5/forecast/daily?lat=' + lat + '&lon=' + long + '&units=imperial&cnt=5&APPID=3acc16ffae9e45df92a064e41646355f', function(json) {
      forecast = json.list;
      showForecast();
    });
  };
  
  var showCurrentWeather = function() {
    var image = tempF > 85 ? '789' : tempF < 85 && tempF > 69 ? '977' : tempF > 55 && tempF < 70 ? '957' : tempF < 55 && tempF > 40 ? '652' : tempF < 40 ? '971' : ''; 
    
    $('#location').html(city + ', ' + country);
    $('#temp').html(tempF + ' &deg;F');
    $('#condition').html(condition);
    
    $('.bg').css('background-image', 'url(https://unsplash.it/1500/1000?image=' + image + ')');
  };
  
  var showForecast = function() {
    var today = new Date().getDate(),
        day, icon, low, high;
    
    forecast.map(function(obj) {
      day = new Date(obj.dt * 1000).getDate() === today ? 'Today' : new Date(obj.dt * 1000).toString().split(' ')[0];
      icon = 'wi wi-owm-' + obj.weather[0].id; // Using erikflowers.github.io/weather-icons/
      low = Math.round(obj.temp.min);
      high = Math.round(obj.temp.max);
      
      // Going to use these for changing the unit on the forecast temps when button is clicked
      lows.push(low);
      highs.push(high);
      
      $('.Weather__forecast-wrap').append(
        '<div class="Weather__forecast-day column column-center">' +
          '<h2>' + day + '</h2>' +
          '<i class="' + icon + '"></i>' +
          '<div class="row">' +
            '<span class="Weather__forecast-high">' + high + '&deg;</span>' +
            '<span class="Weather__forecast-low">' + low + '&deg;</span>' +
          '</div>' +
        '</div>'
      );
    });
  };
  
  var toggleUnit = function() {
    if ( isFar ) {
      
      /** 
      * We go through each span on the page and toggle the temp based on fahrenheit
      * true/false value. I use the lows/highs arrays to get the temps that were
      * originally sent through the api. I stored those temps in the arrays so I
      * could use them down here. 
      **/
      $('.Weather__forecast-high').each(function(index, span) {
        span.innerHTML = Math.round((highs[index] -32) * 5 / 9) + '&deg;';
      });
      
      $('.Weather__forecast-low').each(function(index, span) {
        span.innerHTML = Math.round((lows[index] -32) * 5 / 9) + '&deg;';
      });
      
      // This is for the current weather text
      $('#temp').html(tempC + ' &deg;C');
      $('#toggle').text('Show Fahrenheit');
      
      isFar = false;
    } else { // Same as above, just for fahrenheit
      $('.Weather__forecast-high').each(function(index, span) {
        span.innerHTML = highs[index] + '&deg;';
      });
      
      $('.Weather__forecast-low').each(function(index, span) {
        span.innerHTML = lows[index] + '&deg;';
      });
      
      $('#temp').html(tempF + ' &deg;F');
      $('#toggle').text('Show Celcius');
      isFar = true;
    }
  };
  
  var changeBg = function() {
    var btn = $(this).attr('id');
    var image = btn == 'hot' ? '789' : btn == 'perfect' ? '977' : btn == 'warm' ? '957' : btn == 'cool' ? '652' : btn == 'cold' ? '971' : '';
    
    $('.bg').css('background-image', 'url(https://unsplash.it/1500/1000?image=' + image + ')');
  };    
  
  var reset = function() {
    $('.Weather__forecast-wrap').empty();
    getLocation();
    if ( !isFar ) toggleUnit();
  };
  
  return {
    init: init
  };
})();

weather.init();
              
            
!
999px

Console