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 class="title">
    <h1>JavaScript Weather App</h1>
  </div>
  <div class="location">
    <h1 class="location-time-zone" id="timezone">Please wait while we retrieve the information...</h1>
    <p class="weather-icon">.</p>
  </div>
  <div class="temperature">
    <div class="degree">
      <h2 class="temperature-degree"></h2>
    </div>
    <div class="temp-description">.
    </div>
  </div>

              
            
!

CSS

              
                * {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}
.title {
  font-family: 'Poppins', sans-serif;
  font-size: 14px;
  color: #0a1c29;
}
body {
  height: 100vh;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  background: -webkit-linear-gradient(
    90deg,
    rgba(4, 34, 68, 0.9178046218487395) 17%,
    rgba(0, 212, 255, 1) 100%
  );
  font-family: "Poppins", sans-serif;
  color: #0a1c29;
}
.location {
  align-items: center;
  display: flex;
  width: 50%;
  height: 30vh;
  justify-content: space-around;
  padding: 10px;
}
.location-time-zone{
  font-size:1.5em
}
@media (min-width: 350px) {
  .title {
    margin-top: -30px;
    margin-bottom: 30px;
    font-size: 10px;
  }
  .location {
    width: 30%;
    height: 40vh;
  }
  .location-time-zone {
    padding-right: 120px;
  }
}
.temperature {
  flex-direction: column;
  align-items: center;
  display: flex;
  width: 50%;
  height: 30vh;
  justify-content: space-around;
}
.degree {
  display: flex;
  align-items: center;
  cursor: pointer;
}
.degree span {
  margin: 5px;
  font-size: 20px;
}
.degree h2 {
  font-size: 30px;
}

              
            
!

JS

              
                window.addEventListener("load", () => {
  let temperatureDescription = document.querySelector(".temp-description");
  let temperatureDegree = document.querySelector(".temperature-degree");
  let locationTimeZone = document.querySelector("#timezone");
  let weatherIcon = document.querySelector(".weather-icon");
  let temperature = document.querySelector(".temperature");
  let temperatureP = document.querySelector(".temperature p");
  let title = document.querySelector(".title");
  //App Contants and Variables
  const kelvin = 273;

  const key = "ea9ba620bb5dedeeb7451934f6778f1b";

  //check location
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition((position) => {
      let lon = position.coords.longitude;
      let lat = position.coords.latitude;
      const proxy = "https://cors-anywhere.herokuapp.com/";
      //fetch data from api
      const api = `${proxy}http://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${key}`;
      fetch(api)
        .then((response) => {
          let data = response.json();
          return data;
        })
        .then((data) => {
          temperatureDegree.textContent = `${Math.floor(
            data.main.temp - kelvin
          )}°C`;
          locationTimeZone.textContent = `${data.name}, ${data.sys.country}`;
          temperatureDescription.textContent = data.weather[0].description.toUpperCase();
          let weatherDescription = data.weather[0].description;
          //conditional states for icon option
          weatherImage(weatherDescription);
          //changing background colors
          backgroundColors(weatherDescription);
          //convert kelvins to celsius & fahrenheit
          let celsius = `${Math.floor(data.main.temp - kelvin)}°C`;
          let fahrenheit = `${Math.floor((data.main.temp * 9) / 5 - 459.67)}°F`;
          //add event listener to switch between celsius and fahrenheit
          temperatureDegree.addEventListener("click", function () {
            if (temperatureDegree.textContent === celsius) {
              temperatureDegree.textContent = fahrenheit;
            } else {
              temperatureDegree.textContent = celsius;
            }
          });
        });
    });
  }

  function weatherImage(weatherDescription) {
    switch (weatherDescription) {
      case "broken clouds":
      case "few clouds":
      case "scattered clouds":
        weatherIcon.innerHTML =
          "<i class='wi wi-day-cloudy' style='font-size: 3em'></i>";
        break;
      case "clear sky":
        weatherIcon.innerHTML =
          "<i class='wi wi-day-sunny' style='font-size: 3em'></i>";
        break;
      case "rain":
      case "light rain":
      case "light intensity shower rain":
        weatherIcon.innerHTML =
          "<i class='wi wi-rain-wind' style='font-size: 3em'></i>";
        break;
      case "thunderstorm":
      case "thunderstorm with light rain":
        weatherIcon.innerHTML =
          "<i class='wi wi-thunderstorm' style='font-size: 3em'></i>";
        break;
      case "snow":
        weatherIcon.innerHTML =
          "<i class='wi wi-snow' style='font-size: 3em'></i>";
        break;
      case "mist":
        weatherIcon.innerHTML =
          "<i class='wi wi-fog' style='font-size: 3em'></i>";
        break;
      case "overcast clouds":
      case "squalls":
        weatherIcon.innerHTML =
          "<i class='wi wi-day-windy' style='font-size: 3em'></i>";
    }
  }
  function backgroundColors(weatherDescription) {
    let body = window
      .getComputedStyle(document.body, null)
      .getPropertyValue("background");
    switch (weatherDescription) {
      case "broken clouds":
        body = document.body.style.background = "#6999AD"; //"#fffafa";

        break;
      case "few clouds":
        body = document.body.style.background = "#80CCCC"; //"#fffafa";
        break;
      case "clear sky":
        body = document.body.style.background = "#B2FFFF"; //"#fffafa";
        break;
      case "rain":
      case "light rain":
      case "light intensity shower rain":
      case "thunderstorm with light rain":
        body = document.body.style.background = "#3F465D";
        body = document.body.style.color = "white"; //"#fffafa";
        title.style.color = "white";
        break;
      case "overcast clouds":
        body = document.body.style.background = "#C3EDF1"; //"#fffafa";
        break;
      case "thunderstorm":
        body = document.body.style.background = "#282D41"; //"#fffafa";
        body = document.body.style.color = "white"; //"#fffafa";
        title.style.color = "white";
        break;
      case "snow":
        body = document.body.style.background = "#fffafa"; //"#fffafa";
      case "scattered clouds":
        body = document.body.style.background = "#4E8098"; //"#fffafa";
    }
  }
});

              
            
!
999px

Console