Pen Settings

HTML

CSS

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

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

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.

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="container-fluid">
  <!--<div class="row">
    <div class="col-md-4 col-md-offset-4 col-xs-4 col-xs-offset-4">
      <h1 id="header" class="no-select">Weather App</h1>
    </div>
  </div>-->
  <div class="row center-block">
    <div class="col-md-12 col-xs-12">
      <div id="info-box" class="ui-widget-content">
        <div class="row">
          <div class="col-md-6 col-xs-6">
            <p id="location" class="no-select"></p>
          </div>
          <div class="col-md-6 col-xs-6">
            <p id="condition" class="no-select"></p>
          </div>
        </div>
        <div class="row">
          <div class="col-md-6 col-xs-6">
            <p id="temperature" class="no-select"></p>
          </div>
          <div class="col-md-6 col-xs-6 text-center">
            <i id="icon" class="no-select wi"></i>
          </div>
        </div>
        <div class="row">
          <div class="col-md-6 col-xs-6">
            <p id="temperature-range" class="no-select"></p>
          </div>
          <div class="col-md-6 col-xs-6">
            <p id="updateTime" class="no-select"></p>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                html,
body {
  background-color: #000;
  background-repeat: no-repeat;
  background-size: cover;
  height: 100%;
  overflow: hidden;
}

.container-fluid {
  height: 100%;
  display: flex;
  align-items: center;
}

/*.row {
  border: 3px solid red;
}

.col-md-4, .col-md-6, .col-md-8,
.col-md-12{
  border: 3px solid green;
}*/

.no-select {
  -webkit-touch-callout: none; /* iOS Safari */
  -webkit-user-select: none;   /* Chrome/Safari/Opera */
  -khtml-user-select: none;    /* Konqueror */
  -moz-user-select: none;      /* Firefox */
  -ms-user-select: none;       /* Internet Explorer/Edge */
  user-select: none;           /* Non-prefixed version, currently*/
}

#header {
  color: #FFF;
  font-size: 4em;
  font-family: Montserrat, Arial;
  text-align: center;
  border: 1px solid #666;
  border-radius: 1rem;
  background-color: rgba(0,0,0,0.65);
  width: 630px;
}

#info-box {
  border: 1px solid #666;
  border-radius: 1rem;
  background-color: rgba(0,0,0,0.65);
  /*position: relative;
  top: 300px;*/
  width: 630px;
}

#location {
  color: #BBB;
  font-size: 1.85em;
  padding: 20px;
}

#icon {
  color: #FFF;
  font-size: 9em;
  padding: 30px;
  line-height: 1;
}

#temperature {
  color: #FFF;
  font-size: 9em;
  font-family: Montserrat, Arial;
  text-align: center;
  cursor: pointer;
  padding: 30px;
  margin-bottom: 0px;
  line-height: 1;
}

#temperature-range {
  color: #BBB;
  font-size: 2em;
  text-align: center;
  padding: 10px;
}

#condition {
  color: #BBB;
  font-size: 1.85em;
  font-weight: bold;
  text-align: center;
  padding: 20px;
}

#updateTime {
  color: #BBB;
  font-size: 1.5em;
  padding: 20px;  
}

              
            
!

JS

              
                $(document).ready(function() {

  var cachedLocation = null;
  var cachedWeather = null;
  var scale = "F";

  function getLocation(loc, callback) {
    if (loc !== null) {
      console.log("using cache now...");
      callback(loc);
    } else {
      console.log("fetching location data from API...");
      $.getJSON({
        url: "https://cors-anywhere.herokuapp.com/http://ip-api.com/json",
        success: function(location) {
          storeLocation(location);
          callback(location);
        }
      });
    }
  };

  function storeLocation(location) {
    cachedLocation = location;
  };

  function storeWeather(weather) {
    cachedWeather = weather;
  };

  function getWeatherData(loc) {
    getLocation(loc, function(location) {
      var api = "https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?";
      var apiKey = "APPID=09c93386700062c13adc80faab5f84df";
      var units = "imperial";
      if (location.countryCode !== "US" || scale === 'C') {
        units = "metric"
      };
      var params = "&lat=" + location.lat + "&lon=" + location.lon + "&units=" + units;
      var dataURL = api + apiKey + params;

      //retrieve weather data. only 60 calls/min allowed to API
      $.get(dataURL, function(weatherData) {
        storeWeather(weatherData);
        var condition = weatherData.weather[0].main;
        var iconId = weatherData.weather[0].icon;
        // var randomNum = Math.floor((Math.random() * 3) + 1);
        var randomNum = Math.round((Math.random()));
        var temp = weatherData.main.temp;
        var tempMin = weatherData.main.temp_min;
        var tempMax = weatherData.main.temp_max;
        var iconMap = {
          "01d": "wi-day-sunny",
          "02d": "wi-day-cloudy",
          "03d": "wi-cloud",
          "04d": "wi-cloudy",
          "09d": "wi-day-showers",
          "10d": "wi-day-rain",
          "11d": "wi-day-thunderstorm",
          "13d": "wi-day-snow",
          "50d": "wi-day-fog",
          "01n": "wi-night-clear",
          "02n": "wi-night-alt-cloudy",
          "03n": "wi-cloud",
          "04n": "wi-cloudy",
          "09n": "wi-night-alt-showers",
          "10n": "wi-night-alt-rain",
          "11n": "wi-night-alt-thunderstorm",
          "13n": "wi-night-alt-snow",
          "50n": "wi-night-fog"
        };

//         var conditionMap = {
//           "Additional": "default.jpg",
//           "Atmosphere": "fog_0" + randomNum + ".jpg",
//           "Clear": "clear_0" + randomNum + ".jpg",
//           "Clouds": "clouds_0" + randomNum + ".jpg",
//           "Drizzle": "rain_0" + randomNum + ".jpg",
//           "Extreme": "default.jpg",
//           "Rain": "rain_0" + randomNum + ".jpg",
//           "Snow": "snow_0" + randomNum + ".jpg",
//           "Thunderstorm": "storm_0" + randomNum + ".jpg"
//         };

//         var imageURL = "/images/weather-app/" + conditionMap[condition];

        var conditionMap = {
          "Additional": ["9SFosiF", "9SFosiF"],
          "Atmosphere": ["URXVAvm", "cNWo3WA"],
          "Clear": ["7A01QNp", "sEBiGfu"],
          "Clouds": ["mZod9iJ", "a0XpvA4"],
          "Drizzle": ["FirNVCh", "zMhXZrC"],
          "Extreme": ["9SFosiF", "9SFosiF"],
          "Rain": ["FirNVCh", "zMhXZrC"],
          "Snow": ["aj8rYMe", "cQSGnXU"],
          "Thunderstorm": ["osTgILQ", "mOKXHWK"]
        };
        
        var imageURL = "https://i.imgur.com/" + conditionMap[condition][randomNum] + ".jpg"

        $('#location').text(location.city + ", " + location.region + ", " + location.countryCode);
        $('#temperature').text(Math.round(temp) + "°" + scale);
        $('#temperature-range').text(Math.round(tempMin) + "° / " + Math.round(tempMax) + "°");
        $('#condition').text(condition);
        $('#icon').addClass(iconMap[iconId]);
        $('body').css('background-image', 'url(' + imageURL + ')');

        setUpdateTime();

      });
    });
  };

  function convertTemp(temp, tempMin, tempMax, scale) {
    if (scale === 'F') {
      temp = Math.round((temp - 32) / 1.8);
      tempMin = Math.round((tempMin - 32) / 1.8);
      tempMax = Math.round((tempMax - 32) / 1.8);
      scale = 'C';
      $('#temperature').text(temp + "°" + scale);
      $('#temperature-range').text(tempMin + "° / " + tempMax + "°");
    } else if (scale === 'C') {
      temp = Math.round((temp * 1.8) + 32);
      tempMin = Math.round((tempMin * 1.8) + 32);
      tempMax = Math.round((tempMax * 1.8) + 32);
      scale = 'F';
      $('#temperature').text(temp + "°" + scale);
      $('#temperature-range').text(tempMin + "° / " + tempMax + "°");
    }

    return [temp, tempMin, tempMax, scale];
  };

  function setUpdateTime() {
    var d = new Date;
    var prd = d.getHours() >= 12 ? "PM" : "AM";
    var hr = d.getHours() > 12 ? d.getHours() - 12 :
      d.getHours() == 0 ? d.getHours() + 12 : d.getHours();
    var min = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes();
    $('#updateTime').text("Last updated at " + hr + ":" + min + " " + prd);
  };

  // Get initial weather data
  getWeatherData(cachedLocation);
  
  // Allow Info Box to be draggable
  $('#info-box').draggable();

  // Make temperature clickable and change from F <-> C
  $('#temperature').click(function() {
    var temp = cachedWeather.main.temp;
    var tempMin = cachedWeather.main.temp_min;
    var tempMax = cachedWeather.main.temp_max;
    var arr = convertTemp(temp, tempMin, tempMax, scale);
    cachedWeather.main.temp = arr[0];
    cachedWeather.main.temp_min = arr[1];
    cachedWeather.main.temp_max = arr[2];
    scale = arr[3];
  });

  //Check for weather update every 5mins
  setInterval(function() {
    getWeatherData(cachedLocation);
    setUpdateTime();
  }, 300000);

});
              
            
!
999px

Console