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="outer-container">  

  <div class="data-container text-center container-fluid">
    <div id="conditions"></div>
    <div id="temp"><span id="value"></span> &deg;<span id="units" class="change-temp"></span></div>
    <div id="location"></div>
    <div id="wind"></div>
  </div>
  <div id="controls">
      <div id="change-units" class="change-temp"></div>
      <div id="change-location">Change <br /> Location</div>
  </div>

  <div id="attribution" class="text-center"></div>
  
</div>
              
            
!

CSS

              
                
html {
  background-attachment: fixed;
  background-position: center center;
  background-repeat: no-repeat;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  width: 100vw;
  height: 100vh;
}

html.thunderstorm {
  background-image: url(https://c3.staticflickr.com/4/3680/10913085314_76ef5f7656_b.jpg);
}

html.hail{
  background-image: url(https://c2.staticflickr.com/6/5094/5415991457_46b2267b2e_b.jpg);
}

html.drizzle{
  background-image: url(https://c2.staticflickr.com/9/8296/7984415265_3298a15522_b.jpg);
}

html.snow{
  background-image: url(https://c4.staticflickr.com/5/4018/4486252227_53873b0374_b.jpg);
}

html.fog {
  background-image: url(https://c3.staticflickr.com/9/8439/7959894106_f916023ecc_b.jpg);
}

html.rain {
  background-image: url(https://c2.staticflickr.com/9/8296/7984415265_3298a15522_b.jpg);
}

html.cloudy {
  background-image: url(https://c3.staticflickr.com/9/8217/8306276802_8dfb535661_b.jpg);
}

html.overcast {
  background-image: url(https://c8.staticflickr.com/3/2559/3907212791_f64c59030a_b.jpg);
}

html.def {
  background-image: url(https://c6.staticflickr.com/1/591/22779107021_bd8c1729ba_h.jpg);
}

body {
  font-size: 36px;
  font-family: 'Roboto', sans-serif;
  line-height: 1.42857143;
  color: #fff;
  font-weight: bold;
}

body.rain,
body.overcast,
body.clear,
body.thunderstorm {
  color: #fff;
}

body.ice, 
body.hail,
body.snow,
body.fog,
body.cloudy {
  color: #000;
}

.outer-container {
  position: absolute;
  top: 18%;
  left: 0;
  right: 0;
  margin: auto;
  width: 282px;
  text-align: center;
}

.data-container {
  background: rgba(0,0,0,0.5);
  padding: 20px;
}

.data-container i {
  position: relative;
  font-size: 20px;
}

#wind {
  font-size: 20px;
}

#temp span {
  display: inline;
}

#temp #units {
  color: #d47600;
}

#temp #units:hover {
  cursor: pointer;
  opacity: 0.8;
}

#controls div {
  display: block;
  float: left;
  box-sizing: border-box;
  width: 50%;
  height: 70px;
  font-size: 16px;
  color: #fff;
  text-align: center;
  padding: 10px;
  line-height: 1.5;
}

#controls div:hover {
  cursor: pointer;
}

#controls:after {
  content: '';
  display: block;
  clear: both;
}

#change-units {
  background: rgba(212, 118, 0, 0.6);
}

#change-units:hover {
  background: rgba(212, 118, 0, 1);
}

#change-units:focus {
  outline: none;
}

#change-location {
  background: rgba(0,0,0,0.3);
}

#change-location:hover {
  background: rgba(0,0,0,1);
}

#change-location:focus {
  outline: none;
}

#city-list {
  background-color: #fff;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  color: #444;
  font-size: 1.5rem;
  text-align: left;
  height: 410px;
  overflow-y: auto;
  border: 1px solid #000;
}

#city-list h3 {
  margin: 0;
  padding: 10px 10px 10px 20px;
  background: #555;
  color: #fff;
  position: fixed;
  width: 252px;
  font-size: 1.2rem;
}

#city-list ul {
  list-style: none;
  padding: 0;
  margin-top: 46px;
}

#city-list ul li {
  padding-left: 20px;
  border-bottom: 2px solid #555;
  cursor: pointer;
}

#city-list ul li:first-child {
  font-style: italic;
  font-weight: normal;
}

#city-list ul li:last-child {
  border-bottom: none;
}

#city-list ul li:hover {
  background: #dfdfdf;
}

#attribution {
  position: relative;
  margin-top: 20px;
  display: block;
  width: 100%;
  font-size: 12px;
}

#attribution img {
  max-width: 50px;
}
              
            
!

JS

              
                //Added a few extra locations - could extend this with a more comprehensive and searchable list in the future
var cities = [
  {'city':'Tampa, FL', 'lat':27.9506, 'long':-82.4572},
  {'city':'New York, NY', 'lat':40.77, 'long':-73.98},
  {'city':'Pittsburg, PA', 'lat':40.35, 'long':-79.93},
  {'city':'Atlanta, GA', 'lat':33.65, 'long':-84.42},
  {'city':'Orlando, FL', 'lat':28.5383, 'long':-81.3792},
  {'city':'Dallas, TX', 'lat':32.73, 'long':-96.97},
  {'city':'Jackson, MS', 'lat':32.2988, 'long':-90.1848},
  {'city':'St Louis, MO', 'lat':38.75, 'long':-90.37},
  {'city':'San Diego, CA', 'lat':32.82, 'long':-117.13},
  {'city':'Seattle, WA', 'lat':47.45, 'long':-122.30},
  {'city':'London, England', 'lat':51.47999954, 'long':-0.44999999},
  {'city':'Muscat, Oman', 'lat':23.57999992, 'long':58.27999878},
  {'city':'Moscow, Russia', 'lat':55.83000183, 'long':37.61999893},
  {'city':'Tokyo, Japan', 'lat':35.54999924, 'long':139.77999878},
  {'city':'Cairo, Egypt', 'lat':30.12999916, 'long':31.39999962}
];

var currentWeather = {};

//Get user location from ip-api.com
function getLocation() {
  var xhttp = new XMLHttpRequest();
  var url = "http://ip-api.com/json";
  
  xhttp.onreadystatechange = function() {
    if ( xhttp.readyState == 4 && xhttp.status == 200 ) {
      var locationData = JSON.parse(xhttp.responseText);
      var location = locationData.lat + "," + locationData.lon;
      getWeather(location);
    } else if ( xhttp.status != 200 ) {
      location = '27.9506,-82.4572'; //Tampa FL is the default location if auto-detect fails.
      // TODO - add something in the UI to indicate that this has happened.
      getWeather(location);
    }
  };
  
  xhttp.open("GET", url, true);
  xhttp.send();
}

//Call the WeatherUnderground API with user location
function getWeather(location) {
  var xhttp = new XMLHttpRequest();
  var url = "//api.wunderground.com/api/e1a96d287b019ea8/conditions/q/" + location + ".json";
  var sorry = 'There was an issue retreiving weather data. Please try again in a few minutes.';

  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      var data = JSON.parse(xhttp.responseText);
      if ( data.response.error ) {
        document.getElementsByClassName('data-container')[0].innerHTML = sorry;
      } else {      
        displayWeather(data);
      }
    }
  };
  
  xhttp.open("GET", url, true);
  xhttp.send();
}
  
//Display Weather Data to User
function displayWeather(data) {
  //Set Up Current Weather Object
  currentWeather = {
    conditionsIcon : data.current_observation.icon_url,
    conditions : data.current_observation.weather,
    tempF : data.current_observation.temp_f,
    tempC : data.current_observation.temp_c,
    location : data.current_observation.display_location.full,
    wind : data.current_observation.wind_string,
    attributionImg : data.current_observation.image.url
  }
  
  //Initial weather data to DOM elements
  document.getElementById("conditions").innerHTML = currentWeather.conditions + '<br />' + '<img src="' + currentWeather.conditionsIcon + '" alt="Local Weather Conditions" >';
  document.getElementById("value").innerHTML = currentWeather.tempF;
  document.getElementById("units").innerHTML = 'F';
  document.getElementById("change-units").innerHTML = "Change To Celcius";
  document.getElementById("location").innerHTML = currentWeather.location;
  document.getElementById("wind").innerHTML = "Wind <br />" + currentWeather.wind;
  document.getElementById("attribution").innerHTML = "Weather Data Provided By<br/> <a href=\"https://www.wunderground.com/?apiref=264ec09865b1713d\" target=\"_blank\"><img src=" + currentWeather.attributionImg + " alt=\"Weather Data Provided by WeatherUnderground\" /></a>";

  //Set backgrounds based on weather conditions
  var theBG = document.documentElement;
  if ( /thunderstorm/i.test(currentWeather.conditions) ) {
    theBG.className = 'thunderstorm';
  } else if ( /hail/i.test(currentWeather.conditions) || /ice/i.test(currentWeather.conditions) || /freezing/i.test(currentWeather.conditions) ){
        theBG.className = 'hail';
  } else if ( /drizzle/i.test(currentWeather.conditions) || /mist/i.test(currentWeather.conditions) || /spray/i.test(currentWeather.conditions) ){
        theBG.className = 'drizzle';
  } else if ( /snow/i.test(currentWeather.conditions) ){
        theBG.className = 'snow'; 
  } else if ( /fog/i.test(currentWeather.conditions) || /haze/i.test(currentWeather.conditions) ){
        theBG.className = 'fog';
  } else if ( /rain/i.test(currentWeather.conditions) ){
        theBG.className = 'rain';
  } else if ( /cloudy/i.test(currentWeather.conditions) || /clouds/i.test(currentWeather.conditions) ){
        theBG.className = 'cloudy';
  } else if ( /overcast/i.test(currentWeather.conditions) ){
        theBG.className = 'overcast';
  } else {
        theBG.className = 'def';
  }
}  

//Temperature Value Togglers
var changeTemp = document.getElementsByClassName('change-temp');
for (var i = 0; i < changeTemp.length; i++) {
  changeTemp[i].addEventListener('click', function(e){
    var units = document.getElementById("units");
    var value = document.getElementById("value");
    var temperatureButton = document.getElementById("change-units");
    if ( /f/i.test(units.innerHTML) ) {
        value.innerHTML = currentWeather.tempC;
        units.innerHTML = 'C';
        temperatureButton.innerHTML = "Change To Fahrenheit";
      } else {
        value.innerHTML = currentWeather.tempF;
        units.innerHTML = 'F';
        temperatureButton.innerHTML = "Change To Celcius";
      }
  });
}

//Build list of available cities for user selection
function buildCityList() {
  
  var cityListHeader = document.createElement('h3');
  cityListHeader.appendChild(document.createTextNode('Select Location'));
  
  var cityListContainer = document.getElementsByClassName('data-container');
  var cityList = document.createElement('div');
  cityList.id = 'city-list';
  var ul = document.createElement('ul');
  
  //Make reset selection first
  var resetLi = document.createElement('li');
  resetLi.appendChild(document.createTextNode('Auto-detect')); 
  ul.appendChild(resetLi);
  
  //Add all cities to the list
  for (var i = 0; i < cities.length; i++) {
    var li = document.createElement('li');
    var theCity = document.createTextNode(cities[i].city);
    li.appendChild(theCity);
    ul.appendChild(li);
  }
  cityList.appendChild(cityListHeader);
  cityList.appendChild(ul);
  
  //Send list to the DOM
  cityListContainer[0].appendChild(cityList);
  waitForSelection();
  
}

function waitForSelection() {
  //Attach event listeners for click
    var option = document.querySelectorAll('#city-list li')

    for (var i = 0; i<option.length; i++) {
      option[i].addEventListener('click', function(e) {
        var theSelection = e.target.innerHTML;
        var location = '';
        for (var i = 0; i < cities.length; i++) {
          if ( cities[i].city === theSelection ) {
            location = cities[i].lat + ',' + cities[i].long;
          } else if ( 'Auto-detect' === theSelection ) {
            getLocation();
          }
        }
        
        //Remove city list after selection
        var parent = document.getElementsByClassName('data-container');
        parent[0].removeChild(document.getElementById('city-list'));
        
        //Update weather info
        getWeather(location);
      });
    }
}

//Show list of cities for weather
document.getElementById('change-location').addEventListener('click', function() {
  buildCityList(cities);
});

//Run on Page Load
getLocation();
              
            
!
999px

Console