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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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 id="content">
  <h1>The current local weather for
    <span id="location"></span>,
    <span id="country"></span>:</h1>
  <div id="localConditions">
    <span id="currentCondition"></span>
    <br>
    Temperature: <span id="temp"></span>
    <br>
    Humidity: <span id="humidity"></span>
    <br>
    Wind: <span id="windDirection"></span> at
    <span id="windSpeed"></span>
    <br>
    Visibility: <span id="visibility"></span>
    <br>
  </div>
  <div class="buttons">
    <button id="button">Show Imperial Units</button>
  </div>
  <p><i>Refresh the page to get latest data</i></p>
  <p id="credits">Background images courteousy of <a href="https://pixabay.com/" target="_blank">Pixabay</a></p>
</div>
              
            
!

CSS

              
                body { text-align: center;
       font-family: Arial, Helvetica, sans-serif;
       color: #FFF;
       height: 100%;
       background-repeat: no-repeat;
       background-size: cover; }

h1 { font-size: 48px;
     margin-top: 10%; }

#content { width: 80%;
           height: 100%;
           top: 0;
           right: 0;
           bottom: 0;
           left: 0;
           margin: auto; }

#localConditions { font-size: 36px;
                   line-height: 50px; }

#button { border-radius: 10px;
          padding: 15px;
          max-width: 400px;
          width: 100%;
          font-size: 24px;
          background-color: transparent;
          color: #FFF; }

.buttons { margin-top: 20px; }

#credits { margin-top: 30px; }
              
            
!

JS

              
                $(document).ready(function(){
  
  var apiURL;
  var latitude;
  var longitude;
  
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      latitude = position.coords.latitude;
      longitude = position.coords.longitude;
      
      apiURL = "https://fcc-weather-api.glitch.me/api/current?lat=" + latitude +"&lon=" + longitude;
  
      $.getJSON(apiURL, function(data){
        
        let tempInC = "";
        let tempInF = "";
        let skyCondition = "";
        let windspeedInKPH = "";
        let windspeedInMPH = "";
        let windDirection = "";
        let visibilityInKM = "";
        let visibilityInMiles = "";
        
        $("#location").html(data.name);
        $("#country").html(data.sys.country);
        skyCondition = data.weather[0].description;
        tempInC = Math.round(data.main.temp) + "&deg;C";
        $("#humidity").html(data.main.humidity + "%");
        windspeedInKPH = Math.round((data.wind.speed * 3.6)) + " km/h";
        windDirection = data.wind.deg;
        visibilityInKM = Math.round((data.visibility * 0.001)) + " km";
        
        showSkyCondition(skyCondition);
        showWindDirection(windDirection);
        showMetricUnits();
        
        let button = $("#button");
        button.on("click", function(){
          if(button.html() == "Show Imperial Units")
          {
            button.html("Show Metric Units");
            showImperialUnits(tempInC, windspeedInKPH, visibilityInKM);
          }
          else
          {
            button.html("Show Imperial Units");
            showMetricUnits();
          }
        }); // End of anonymous button click function
        
        function showMetricUnits()
        {          
          $("#temp").html(tempInC);
          $("#windSpeed").html(windspeedInKPH);
          $("#visibility").html(visibilityInKM);
        }

        function showImperialUnits(celsiusTemp, kilometersPerHour, kilometers)
        {
          tempInF = parseFloat(celsiusTemp) * 1.8 + 32;
          windspeedInMPH = parseFloat(kilometersPerHour) / 1.61;
          visibilityInMiles = parseFloat(kilometers) / 1.61;
          
          $("#temp").html(Math.round(tempInF) + "&deg;F");
          $("#windSpeed").html(Math.round(windspeedInMPH) + " mph");
          $("#visibility").html(Math.round(visibilityInMiles) + " miles");
        }

        function showSkyCondition(skyAndOrPrecip)
        {
          switch(skyAndOrPrecip){
            case "clear sky":
              $("#currentCondition").html("Clear");
              $("body").css("background-image", "url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/clear_sky.jpg)");
              break;
            case "scattered clouds":
            case "few clouds":
            case "broken clouds":
              $("#currentCondition").html("Partly Cloudy");
              $("body").css("background-image", "url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/partly-cloudy.jpg)");
              $("body").css("color", "#000");
              $("#button").css("color", "#000");
              break;
            case "overcast clouds":
              $("#currentCondition").html("Overcast");
              $("body").css("background-image", 'url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/overcast.jpg)');
              break;
            case "haze":
              $("#currentCondition").html("Haze");
              $("body").css("background-image", 'url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/hazy.jpg)');
              break;
            case "mist":
            case "light intensity drizzle":
            case "drizzle":
              $("#currentCondition").html("Drizzle");
              $("body").css("background-image", 'url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/drizzle.jpg)');
              break;
            case "light rain":
              $("#currentCondition").html("Light Rain");
              $("body").css("background-image", 'url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/rain.jpg)');
              break;
            case "moderate rain":
              $("#currentCondition").html("Moderate Rain");
              $("body").css("background-image", 'url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/rain.jpg)');
              break;
            case "heavy intensity rain":
              $("#currentCondition").html("Heavy Rain");
              $("body").css("background-image", 'url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/heavy_rain.jpg)');
              $("body").css("color", "#000");
              break;
            case "thunderstorm":
              $("#currentCondition").html("Thunderstorm");
              $("body").css("background-image", 'url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/rain.jpg)');
              break;
            default:
              $("#currentCondition").html("Current sky condition and/or precip unknown");
              $("body").css("color", "#000");
          }
        } // End of showSkyCondition(...) function
        
        function showWindDirection(direction)
        {
          if(direction >= 0 && direction < 20)
            $("#windDirection").html("N");
          else if(direction >= 20 && direction < 40)
            $("#windDirection").html("NNE");
          else if(direction >= 40 && direction < 60)
            $("#windDirection").html("NE");
          else if(direction >= 60 && direction < 85)
            $("#windDirection").html("ENE");
          else if(direction >= 85 && direction < 95)
            $("#windDirection").html("E");
          else if(direction >= 95 && direction < 120)
            $("#windDirection").html("ESE");
          else if(direction >= 120 && direction < 150)
            $("#windDirection").html("SE");
          else if(direction >= 150 && direction < 175)
            $("#windDirection").html("SSE");
          else if(direction >= 175 && direction < 185)
            $("#windDirection").html("S");
          else if(direction >= 185 && direction < 230)
            $("#windDirection").html("SSW");
          else if(direction >= 230 && direction < 250)
            $("#windDirection").html("SW");
          else if(direction >= 250 && direction < 265)
            $("#windDirection").html("WSW");
          else if(direction >= 265 && direction < 275)
            $("#windDirection").html("W");
          else if(direction >= 275 && direction < 310)
            $("#windDirection").html("WNW");
          else if(direction >= 310 && direction < 330)
            $("#windDirection").html("NW");
          else if(direction >= 330 && direction < 355)
            $("#windDirection").html("NNW");
          else if(direction >= 355 && direction <= 360)
            $("#windDirection").html("N");
          else
            $("#windDirection").html("unknown direction");
        } // End of showWindDirection(...) function   
        
        // setTimeout(function(){
        //   window.location.reload();
        // }, 60000);
        
      }); // End of getJSON(...) function
    }); // End of navigator getCurrentPosition function
  } // End of if(navigator....) statement
});


              
            
!
999px

Console