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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <body onload="updateClock(); setInterval('updateClock()', 1000 )">

  <div class="container">
    <div class="row" id="head">
      <div class="col-12">OpenWeatherMap API</div>
    </div>

    <div class="row" id="location">
      <div class="col-6"><span id="userLocation">Waiting for location...</span></div>
      <div class="col-6" id="time"><span id="clock">&nbsp;</span></div>
    </div>

    <div class="row">
      <div class="col-6" id="weatherIcon">
        <img id="weatherIcon">
      </div>
      <div class="col-6" id="weatherType">
        <p><span id="userWeather"></span></p>
      </div>
    </div>

    <div class="row">
      <div class="col-12" id="weatherTemp">
        <p><span id="userTemp">??</span></p>
      </div>
    </div>

    <div class="row">
      <div class="col-2">Today</div>
      <div class="col-10" id="tempRange">
        <span id='high-low'></span>
      </div>
    </div>

    <div class="row">
      <div class="col-6">Humidity</div>
      <div class="col-6"><span id="userHumid">??</span>%</div>
    </div>

    <div class="row">
      <div class="col-6">Wind Speed</div>
      <div class="col-6"><span id="userWind">??</span> MPH</div>
    </div>

    <div class="row">
      <div class="col-8" id="units">Imperial/Metric:</div>
      <div class="col-4" id="switch">
        <input id="cmn-toggle-1" class="cmn-toggle cmn-toggle-round" type="checkbox" checked="checked">
        <label for="cmn-toggle-1"></label>
      </div>
    </div>

  </div>

  <footer class="text-center">
    <p>OpenWeatherMap API app coded through <a target="_blank" href="">Open Source</a></p>
  </footer>

</body>
              
            
!

CSS

              
                .container {
  font-family: Arial, Helvetica, sans-serif;
  height: 400px;
  width: 270px;
  margin-top: 15px;
  border: 2px solid #908b85;
  border-radius: 20px;
  box-shadow: 7px 10px 34px 1px rgba(0, 0, 0, 0.68), inset -1px -6px 12px 0.1px #89847e;
}

.row {
  padding: 3px;
}

#head {
  text-align: center;
}

#location {
  font-size: 18px;
}

#time {
  text-align: center;
}

#clock {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 18px;
  color: orange;
}

#weatherIcon {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
}

#weatherTemp {
  font-size: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

#weatherType {
  /*background-color: grey;*/
  /*padding: 5px 0px;*/
  display: flex;  
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  text-align: center;
}

#tempRange {
  text-align: center;
}

#units {
  text-align: right;
}

#switch {
  padding: 5px;
}


/* toggle switch by Call Me Nick - http://callmenick.com/post/css-toggle-switch-examples */

.cmn-toggle {
  position: absolute;
  margin-left: -9999px;
  visibility: hidden;
}

.cmn-toggle + label {
  display: block;
  position: relative;
  cursor: pointer;
  outline: none;
  user-select: none;
}

input.cmn-toggle-round + label {
  padding: 2px;
  width: 40px;
  height: 20px;
  background-color: #dddddd;
  border-radius: 20px;
}

input.cmn-toggle-round + label:before,
input.cmn-toggle-round + label:after {
  display: block;
  position: absolute;
  top: 1px;
  left: 1px;
  bottom: 1px;
  content: "";
}

input.cmn-toggle-round + label:before {
  right: 1px;
  background-color: #f1f1f1;
  /* content: attr(data-on); */
  border-radius: 20px;
  transition: background 0.4s;
  /* backface-visibility: hidden; */
}

input.cmn-toggle-round + label:after {
  width: 19px;
  background-color: #fff;
  /* content: attr(data-off); */
  border-radius: 100%;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  transition: margin 0.4s;
  /* backface-visibility: hidden; */
}

input.cmn-toggle-round:checked + label:before {
  background-color: #8ce196;
}

input.cmn-toggle-round:checked + label:after {
  margin-left: 20px;
}
              
            
!

JS

              
                // Clock start
function init() {
  timeDisplay = document.createTextNode("");
  document.getElementById("clock").appendChild(timeDisplay);
}

function updateClock() {
  var currentTime = new Date();

  var currentHours = currentTime.getHours();
  var currentMinutes = currentTime.getMinutes();
  var currentSeconds = currentTime.getSeconds();

  // Pad the minutes and seconds with leading zeros, if required
  currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
  currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;

  // Choose either "AM" or "PM" as appropriate
  var timeOfDay = (currentHours < 12) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = (currentHours == 0) ? 12 : currentHours;

  // Compose the string for display
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

  // Update the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
// Clock end

// Weather API start
$(document).ready(function() {
  var OWM_API_KEY = "b63b3237025e950cc1400adbae48a26c ";
  var cel = false;
  var localWeather;

  // celcius to farenheit conversions

  function displayTemp(fTemp, c) {
    if (c) return Math.round((fTemp - 32) * (5 / 9)) + " &#8451 ";
    return Math.round(fTemp) + " &#8457 ";
  }

  // setting vars and elements

  function render(localWeather, cel) {
    var userLocation = localWeather.name;
    var userWeather = localWeather.weather[0].description;
    var userTemp = displayTemp(localWeather.main.temp, cel);
    var userHumid = localWeather.main.humidity;
    var userWind = localWeather.wind.speed;
    var high = displayTemp(localWeather.main.temp_max, cel);
    var low = displayTemp(localWeather.main.temp_min, cel);
    var icon = localWeather.weather[0].icon;

    $('#userLocation').html(userLocation);
    $('#userTemp').html(userTemp);
    $('#userWeather').html(userWeather);
    $('#userHumid').html(userHumid);
    $('#userWind').html(userWind);
    $('#high-low').html("High " + high + " / Low " + low);

    var iconSrc = "http://openweathermap.org/img/w/" + icon + ".png ";
    $('#weatherIcon').html('<img src=" ' + iconSrc + ' " width="82" height="82">');
  }

  // get users lat/long

  var getPosition = {
    enableHighAccuracy: false,
    timeout: 7000,
    maximumAge: 0
  };

  function success(gotPosition) {
    var uLat = gotPosition.coords.latitude;
    var uLon = gotPosition.coords.longitude;
    console.log(`${uLat}`, `${uLon}`);

    // now put the users lat/long into the OpenWeatherMap API Query

    $.getJSON('http://api.openweathermap.org/data/2.5/weather?units=imperial&lat=' + uLat + '&lon=' + uLon + '&APPID=' + OWM_API_KEY,
      function(OWM_API_DATA) {
        localWeather = OWM_API_DATA;
        console.log(localWeather);
        render(OWM_API_DATA, cel);

        $(':checked').click(function() {
          cel = !cel;
          render(localWeather, cel);
        })
      })

  };

  function error(err) {
    console.warn(`ERROR(${err.code}): ${err.message}`);
  };

  navigator.geolocation.getCurrentPosition(success, error, getPosition);
})
              
            
!
999px

Console