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

              
                <html lang="en">

<head>
  <title>LocalWeather</title>
  <meta charset="UTF-8">
  <meta name="description" content="fcc-local-weather">
  <meta name="keywords" content="HTML,CSS,JavaScript, Weather">
  <meta name="author" content="Nicholas Galarza">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" type="text/css" href="css/style.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link href="https://fonts.googleapis.com/css?family=Khula" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
</head>

<body id="main">
  <div class="bg-image"></div>

  <div class="main-content">

    <div id="main-weather-info">
      <!-- Container for weather conditions. -->
      <div class="weather-block">
        <!-- Make the bg box transparent -->
        <div class="block-opacity"></div>
        <div class="location">
          <i id="l-icon" class="fa fa-globe fa-2x" aria-hidden="true"></i>
          <p id="location">Location</p>
        </div>
        <div class="date-time">
        </div>

        <canvas class="box" id="icon"></canvas>
        <!-- Weather Icon & temperature -->
        <div class="box">
          <div class="row">
            <!-- date -->
            <div class="date">
              <div id="day-of-week"></div>
              <div id="date"></div>
            </div>
            <!-- vertical line -->
            <div id="vertical-line"></div>
            <!-- temperature -->
            <div class="temperature">
              <div id="temperature"></div>
              <div id="measure"></div>
            </div>
            <!--
                        <div id="high"></div>
                        <div class="inlineSlash">&#176; / </div>
                        <div id="low"></div>
                        <div class="inlineSlash">&#176;</div>
                        <div id="type">F</div>
                      -->
          </div>
        </div>
        <!-- end of flex box -->
      </div>
      <!-- Rounded switch -->
      <div id="switch-button">

        <p id="F">F</p>
        <label class="switch">
                  <input type="checkbox">
                  <div class="slider round"></div>
                  </label>
        <p id="C">C</p>
      </div>
    </div>

  </div>
  <!-- end of main content -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://rawgithub.com/darkskyapp/skycons/master/skycons.js"></script>
  <script type="text/javascript" src="js/weather.js"></script>

  <body>

</html>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Khula');
@import url('https://fonts.googleapis.com/css?family=Roboto+Mono');
* {
  margin: 0 auto;
}

body, html {
  height: 100%;
  overflow: hidden;
}

.bg-image {
  /* Background image used */
  background-image: url("https://4.bp.blogspot.com/-24x23Te7b_0/VWcHnvt-5VI/AAAAAAABjT0/TzjTu3JmLKo/s0/Manhattan_Empire_State_Building_uhd.jpg");
  /* Center & sclare the image */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  /* full height */
  height: 100%;
  /* Prevent blur from affecting all child elements */
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  filter: blur(0px);
  z-index: -1;
}

#main {
  background-color: #474747;
}

#main .main-content {
}

/* Weather-block & block-opacity are utility classes for containing boxes */

.weather-block {
  margin-top: 10px;
  padding: 10px;
  color: snow;
  font-weight: bold;
  border-radius: 10px;
  position: relative;
}

.block-opacity {
  box-shadow: 5px 5px 15px black;
  border: 1px solid #2d2d2d;
  border-radius: 10px;
  /* Prevent opacity from fading text */
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #2d2d2d;
  opacity: .80;
  z-index: -1;
}

.location {
  margin-bottom: 10px;
  margin-top: 5px;
  display: inline-block;

  text-align: center;
  width: 100%;
}

.location p {
  font-size: 20px;
  display: inline;
  color: snow;
}

.location i {
  margin-right: 5px;
  color: #ff9000;
}

#main-weather-info {
  padding: 10px;
  width: 90%;
}

#main-weather-info .box {
  text-align: center;
  margin-bottom: 20px;
  display: block;
}

#main-weather-info canvas {
  text-align: center;
}

#main-weather-info p {
  color: snow;
  font-weight: bold;
  font-family: 'Khula', sans-serif;
  font-size: 20px;
}

.date #day-of-week {
  color: snow;
  font-size: 25px;
}

.date #date {
  color: #ff9000;
  font-size: 20px;
}

.temperature #temperature, #measure {
  color: snow;
  font-size: 40px;
  display: inline;
}

#main-weather-info #temperature,
#high,
#low,
#type,
#precipType,
#precipChance,
#windSpeed,
#humidity,
#summary,
#vertical-line,
.inlineSlash {
  display: inline-block;
}

/* General location of toggle button */

#switch-button {
  margin-top: 3px;
  text-align: center;
  position: relative;
  background-color: snow;
  border-radius: 10px;
}

#switch-button p {
  color: #2d2d2d;
  margin: 8px;
  font-size: 40px;
  font-weight: bolder;
  display: inline;
}

/* The switch - the box around the slider */

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

/* Hide default HTML checkbox */

.switch input {
  display: none;
}

/* The slider */

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #2d2d2d;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: snow;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2d2d2d;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2d2d2d;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

/* End of switch button */

/* Desktop media query */

@media (min-width: 400px) {
  #main-weather-info {
    width: 400px;
  }
  .row {
    display: block;
  }
  #vertical-line {
    border-left: medium snow solid;
    height: 50px;
    display: inline-block;
    margin-left: 25px;
    margin-right: 25px;
  }

  .date {
    display: inline-block;
  }

  .temperature {
    display: inline-block;
  }
}

@media (min-width: 768px) {
}

              
            
!

JS

              
                "use strict";
$(document).ready(function() {
  var temperature, highTemp, lowTemp, icon;
  var skycons = new Skycons({
    color: "snow"
  });

  var today = new Date(),
    dd = today.getDate(),
    mm = new Array(
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "Octotober",
      "November",
      "December"
    ),
    weekday = new Array(
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday"
    ),
    dayOfWeek = weekday[today.getDay()],
    month = mm[today.getMonth()];

  // Render day of week & current date.
  $("#day-of-week").text(dayOfWeek);
  $("#date").text(month + ", " + dd);

  // 1.) HTML5 Geolocation: This is built into the browser pretty much and all getCoordinates()
  // does is returns a 'reference' to the API so one can access information at a later point.
  // If the browser does not support geolocation, I'm just sending a little message saying this
  // program won't work lol. Next part to explain is at the end of this code so scroll all the way down!
  function getCoordinates() {
    if (navigator.geolocation) {
      return navigator.geolocation;
    } else {
      alert(
        "Failed to retrieve your coordinates because HTML5 Geolocation is not supported on your browser."
      );
    }
  }

  // 4.) Now here is the callback function that I mentioned before. For consistency, I named the method 'success', and
  // I named the reference which will contain important location information 'position'. You could name the function and
  // intaking parameter whatever you like. I only choose these names for consistency with the API documentation. But
  // remember, our callback will recieve a Position object (we know this bc the documentation says it will), therefore,
  // we are saving the 'reference' of the Position object generated in getCurrentPosition() and saving it in the parameter
  // called position because that is how we will extract the latitude and longitude as seen below.
  function success(position) {
    var tempArr = [];
    var latitude = position.coords.latitude,
      longitude = position.coords.longitude,
      key = "84d9272c2a6c08552f1e4fb558f1a98d";

    // getJSON() is a shorthand to ajax() and allows you to get the JSON object straight away.
    // additionally, "callback=?" is appended because this is making an external request and
    // treats the GET request as JSONP to work around cross site limitations.
    $.getJSON(
      "https://api.darksky.net/forecast/" +
        key +
        "/" +
        latitude +
        "," +
        longitude +
        "?callback=?",
      function(forecast) {
        // Extract values for current temperature.
        temperature = Math.floor(forecast.currently.temperature);
        highTemp = Math.floor(forecast.daily.data[0].apparentTemperatureMax);
        lowTemp = Math.floor(forecast.daily.data[0].apparentTemperatureMin);
        icon = forecast.currently.icon;

        switch (icon) {
          case "clear-day":
            skycons.add(document.getElementById("icon"), Skycons.CLEAR_DAY);
            break;
          case "clear-night":
            skycons.add(document.getElementById("icon"), Skycons.CLEAR_NIGHT);
            break;
          case "partly-cloudy-day":
            skycons.add(
              document.getElementById("icon"),
              Skycons.PARTLY_CLOUDY_DAY
            );
            break;
          case "partly-cloudy-night":
            skycons.add(
              document.getElementById("icon"),
              Skycons.PARTLY_CLOUDY_NIGHT
            );
            break;
          case "cloudy":
            skycons.add(document.getElementById("icon"), Skycons.CLOUDY);
            break;
          case "rain":
            skycons.add(document.getElementById("icon"), Skycons.RAIN);
            break;
          case "sleet":
            skycons.add(document.getElementById("icon"), Skycons.SLEET);
            break;
          case "snow":
            skycons.add(document.getElementById("icon"), Skycons.SNOW);
            break;
          case "wind":
            skycons.add(document.getElementById("icon"), Skycons.WIND);
            break;
          default:
            skycons.add(document.getElementById("icon"), Skycons.FOG);
        }
        skycons.play();

        var humidity = forecast.currently.humidity,
          summary = forecast.currently.summary,
          precipChance = forecast.currently.precipProbability,
          windSpeed = forecast.currently.windSpeed,
          location = forecast.timezone;

        // Apply values to the HTML.
        $("#location").text(prettify(location));
        $("#temperature").text(temperature);
        $("#high").text(highTemp);
        $("#low").text(lowTemp);
        $("#precipChance").text(precipChance);
        $("#summary").text('"' + summary + '"');
        $("#windSpeed").text(windSpeed);
        $("#humidity").text(humidity);
        $("#measure").html("&#176;F");
        console.log(forecast);
      }
    );

    function prettify(string) {
      return string.replace(/[/]/g, ", ").replace(/[_]/g, " ").toUpperCase();
    }

    // Button conversion for Fahrenheight to Celcius.
    var fahrenheight = true; // is Fahrenheight.
    $(".slider").click(function() {
      // Convert to Celcius.
      if (fahrenheight) {
        $("#temperature")
          .hide()
          .fadeIn(2000)
          .text(Math.floor((temperature - 32) * (5 / 9)));
        $("#high")
          .hide()
          .fadeIn(2000)
          .text(Math.floor((highTemp - 32) * (5 / 9)));
        $("#low")
          .hide()
          .fadeIn(2000)
          .text(Math.floor((lowTemp - 32) * (5 / 9)));
        $("#measure").hide().fadeIn(2000).html("&#176;C");
        fahrenheight = false;
      } else {
        fahrenheight = true;
        $("#temperature").hide().fadeIn(2000).text(temperature);
        $("#high").hide().fadeIn(2000).text(highTemp);
        $("#low").hide().fadeIn(2000).text(lowTemp);
        $("#measure").hide().fadeIn(2000).html("&#176;F");
      }
    });
  }
  //2.) Over here, I'm saving the reference of the geolocation api and saving it in geoNavigator.
  var geoNavigator = getCoordinates();
  // Then I'm using .getCurrentPosition() which is a method in the geonavigator API, and if geonav is
  // supported, then you can pass in a function. Now, in JavaScript, functions can also be
  // treated as values, which is very odd ... I know. But here's what you need to know in this instance:
  // Since .getCurrentPosition() works, you can pass in a function and it will have a parameter called
  // position, and position is also a 'reference' generated by the API so that you can access its values.
  // Documentation on getCurrentPosition: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition
  geoNavigator.getCurrentPosition(success);

  // 3.) Some lingo to explain.
  // What is a reference? It is when a variable refers to an object.
  // What is a callback function? It is when a function is passed to another function as a parameter.
});

              
            
!
999px

Console