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 id="outer">
  <div id="container">
    
    <div id="loading" class="text-center">
      <p id="load-status">Your weather is loading...</p>
    </div>

    <div id="http-alert-message" class="error text-center">I'm using your IP address to determine your location. Use HTTPS and enable location services for a more accurate reading.</div>
    
    <div id="main-title" class="text-center to-toggle">
      <h2>Your local</h2>
      <h1>WeatherApp</h1>
    </div>
    
        <div id="weather-data-div" class="text-center">

          <div id="city-name" class="to-toggle">
          <!--City name goes here-->
          </div>


      <div class="temperature">
        <button id="unit-change" type="button" class="btn btn-primary to-toggle">
        <!--Temperature and unit change button go here-->
    </div>

  

      <div id = "condition" class = "to-toggle">
      <!--Current condition goes here-->
    </div>

  

    <div id = "condition-icon" class = "to-toggle">
      <!--Condition icon goes here-->
    </div>
      
      <div id = "search" class = "input-group add-on to-toggle">
        <input type="text" class="form-control" placeholder="Other location (ZIP or city name)" onfocus= "" id="search-value">
        
        <div class="input-group-btn">
          <button id="search-button" class="btn btn-default"><i class="glyphicon glyphicon-chevron-right"></i></button>
      </div>
    </div>

    <div id="search-error-message" class="error text-center">Sorry, I'm unable to find that place.</div>

    <div id="attribute" class="text-center to-toggle">
      <br>
      <p>by <a href="https://www.linkedin.com/in/bentongarrett" target="_blank">Benton Garrett</a></p>
    </div>

  </div>
</div>
</div>
  
<div id="preload">
   <img src="https://s32.postimg.org/edyc7gjk5/tstorm.jpg" width="1" height="1" alt="Image 01" />
   <img src="https://s32.postimg.org/t631ysphh/lightrain.jpg" width="1" height="1" alt="Image 02" />
   <img src="https://s32.postimg.org/41c1ldq11/rain.jpg" width="1" height="1" alt="Image 03" />
   <img src="https://s32.postimg.org/agb2i1wqt/snow.jpg" width="1" height="1" alt="Image 04" />
   <img src="https://s32.postimg.org/q9g0yijnp/clearsky.jpg" width="1" height="1" alt="Image 05" />
   <img src="https://s32.postimg.org/xqp8dq96t/cloudy.jpg" width="1" height="1" alt="Image 06" />
</div>
            
          
!
            
              html,
body {
  width: 100%;
  height: 100%;
  font-family: 'Oxygen';
}

#outer {
  position: relative;
  width: 100%;
  height: 100%;
  background: #F8F8F8;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  transition: background .25s linear;
}

#outer #container {
  background-color: #fff;
  color: #000;
  padding: 15px 25px;
  width: 100%;
  max-width: 350px;
  position: absolute;
  transform: translate(-50%, -50%);
  left: 50%;
  top: 50%;
  opacity: 0.85;
}

h1 {
  color: #505050;
  font-size: 48px;
  margin: 0px 0px 30px 0px;
}

h2 {
  color: #505050;
  margin: 10px 0px 0px 0px;
}

p {
  font-weight: bold;
}

.to-toggle {
  display: none;
}

#city-name {
  font-size: 30px;
  font-weight: 700;
  margin: 0px 0px 20px 0px;
}

.temperature {
  font-size: 22px;
  text-align: center;
  padding: 10px 5px 0px 8px;
}

img.expand {
  width: 7em;
}

#condition {
  text-transform: capitalize;
}

.error {
  color: #fff;
  background-color: #AA3939;
  display: none;
}

.add-on .input-group-btn > .btn {
  border-left-width: 2;
  left: -2px;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  opacity: 0.85;
}

.add-on .form-control {
  text-align: center;
  width: 260px;
}


/*Placeholder text styling*/

::-webkit-input-placeholder {
  text-align: center;
}

:-moz-placeholder {
  /* older Firefox*/
  text-align: center;
}

::-moz-placeholder {
  /* Firefox 19+ */
  text-align: center;
}

:-ms-input-placeholder {
  text-align: center;
}
            
          
!
            
              /*Chrome now has issues with cross origin HTTP/S requests.
To compensate, this script determines if the user is accessing the site via HTTP or HTTPS.
If HTTPS, the geolocation request must go through another server, cors-anywhere.herokuapp.com, which will make it work.
If HTTP, the location will be pulled from the user's IP. This is less accurate than the geolocation request as above, but it will function without the user needing to change the URL string to HTTPS.
*/
function httpsLocationURL() {
    navigator.geolocation.getCurrentPosition(function(position) {
    var latitudeGEO = position.coords.latitude;
    var longitudeGEO = position.coords.longitude;

    //Construct the necessary OpenWeather URL
    var weatherURL1 = 'https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?lat=';
    var weatherURL2 = '&lon=';
    var weatherURL3 = '&APPID=4bee96f340bd4c16c3b476b8848df9ae';
    var weatherURL = weatherURL1 + latitudeGEO + weatherURL2 + longitudeGEO + weatherURL3;
    console.log('HTTPS URL was constructed. applyWeatherInformation function is next.');
    console.log('Constructed URL: ' + weatherURL);

    applyWeatherInformation(weatherURL);
});
}

function httpLocationURL() {
    $.getJSON('http://ip-api.com/json', function(data) {
    
    //Alert the user that they are using HTTP, which may be less accurate
    $("#http-alert-message").show().delay(6000).fadeOut();
    
    //Log the JSON data
    console.log(data);
      
    var latitudeIP = data.lat;
    var longitudeIP = data.lon;
    console.log('User longitude: ' + longitudeIP);
    console.log('User latitude: ' + latitudeIP);

    //Construct the necessary OpenWeather URL
    var weatherURL1 = 'http://api.openweathermap.org/data/2.5/weather?lat=';
    var weatherURL2 = '&lon=';
    var weatherURL3 = '&APPID=4bee96f340bd4c16c3b476b8848df9ae';
    var weatherURL = weatherURL1 + latitudeIP + weatherURL2 + longitudeIP + weatherURL3;
    console.log('HTTP URL was constructed. applyWeatherInformation function is next.');
    console.log('Constructed URL: ' + weatherURL);

    applyWeatherInformation(weatherURL);
  });
}

if ("https:" == document.location.protocol) {
  httpsLocationURL();
} else if ("http:" == document.location.protocol) {
  httpLocationURL();
}

/*************
This function pulls the JSON information.
*************/
function applyWeatherInformation(weatherURL) {
  console.log('applyWeatherInformation function called. getJSON is next.');
  //Get OpenWeather JSON data and apply to HTML
  $.getJSON(weatherURL, function(weatherCall) {

    /*Error handling, mostly for explicit search.
    If the API doesn't return a city name, it's safe to assume it can't find one matching the search value.
    This code will unhide the previous search results, show an error message, and stop the function.
    */
    if (weatherCall.name == undefined) {
      $('.to-toggle').show(150, "swing");
      $("#search-error-message").show().delay(6000).fadeOut();
      return;
    }

    //Convert temperatures from Kelvin to Fahrenheit and Celsius
    var tempF = (((weatherCall.main.temp * 1.8) - 459.67).toFixed(1));
    var tempC = (((weatherCall.main.temp) - 273.15).toFixed(1));

    console.log('getJSON called. jQuery updates are next.');
    console.log(weatherCall);
    //Make relevant JSON information more readable
    var userCity = weatherCall.name;
    var userCountry = weatherCall.sys.country;
    var conditionDesc = weatherCall.weather[0].description;
    var conditionCode = weatherCall.weather[0].id;
    var iconCode = weatherCall.weather[0].icon;

    //fadeOut loading message
    $('#loading').fadeOut(25);

    //Apply information to HTML and show elements with animation
    $('#city-name').html('<p>' + userCity + ', ' + userCountry + '</p>').show(150, "swing");
    $('#unit-change').html('<p class="temperature">' + tempF + '<strong>°F</strong<></p>').show(150, "swing");
    $('#condition').html('<p>' + conditionDesc + '</p>').show(150, "swing");
    $('#condition-icon').html('<img class = "expand" src = "http://openweathermap.org/img/w/' + iconCode + '.png">').show(150, "swing");

    $('#main-title').show(150, "swing");
    $('#attribute').show(150, "swing");
    $('#search').show(150, "swing");

    //Temperature unit change button handler
    var isF = true;
    $("#unit-change").on("click", function() {
      if (isF) {
        $('#unit-change').html('<p class="temperature">' + tempC + '<strong>°C</strong></p>');
        isF = false;
        console.log('Temperature is currently displayed in Celsius.');
      } else {
        $('#unit-change').html('<p class="temperature">' + tempF + '<strong>°F</strong></p>');
        isF = true;
        console.log('Temperature is currently displayed in Fahrenheit.');
      }
    });

    //Weather background changer
    if (conditionCode >= 200 && conditionCode <= 232) {
      $('#outer').css('background-image', 'url("https://s32.postimg.org/edyc7gjk5/tstorm.jpg")');
    } else if (conditionCode >= 300 && conditionCode <= 321) {
      $('#outer').css('background-image', 'url("https://s32.postimg.org/t631ysphh/lightrain.jpg")');
    } else if (conditionCode >= 500 && conditionCode <= 531) {
      $('#outer').css('background-image', 'url("https://s32.postimg.org/41c1ldq11/rain.jpg")');
    } else if (conditionCode >= 600 && conditionCode <= 622) {
      $('#outer').css('background-image', 'url("https://s32.postimg.org/agb2i1wqt/snow.jpg")');
    } else if (conditionCode >= 700 && conditionCode <= 781) {
      $('#outer').css('background-image', 'url("https://s29.postimg.org/h3x3kz09z/Mist.jpg")');
    } else if (conditionCode == 800) {
      $('#outer').css('background-image', 'url("https://s32.postimg.org/q9g0yijnp/clearsky.jpg")');
    } else if (conditionCode >= 801 && conditionCode <= 804) {
      $('#outer').css('background-image', 'url("https://s32.postimg.org/xqp8dq96t/cloudy.jpg")');
    }

  });
}

//Reusable code for an explicit search
function explicitSearch() {
  //Hide the previous weather result
  $('.to-toggle').hide(150, "swing");
  
  var searchValue = document.getElementById('search-value').value;
  console.log('String value click');
  
  var weatherURL = 'https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?q=' + searchValue + '&APPID=4bee96f340bd4c16c3b476b8848df9ae';
  console.log('Search URL is ' + weatherURL);
  
  //Clear out the input field upon button click
  $('input[type="text"]').val('');

  applyWeatherInformation(weatherURL);
}

//Click handler for explicit search button
$('#search-button').click(explicitSearch);

//Send enter key to explicit search button click handler
$('#search').keypress(function(e) {
  var code = (e.keyCode ? e.keyCode : e.which);
  if (code == 13) {
    $('#search-button').click();
  }
});

/*This removes the search box placeholder text when the field is in focus.
Conversely, it shows the placeholder when the field is not in focus.
Credit for this bit goes to Rohan Kumar at StackSocial (http://stackoverflow.com/questions/19676084/how-to-remove-placeholder-on-focus). Thanks!*/
$('input,textarea').focus(function(){
   $(this).data('placeholder',$(this).attr('placeholder'))
          .attr('placeholder','');
}) .blur(function(){
   $(this).attr('placeholder',$(this).data('placeholder'));
});
            
          
!
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