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

              
                	<main>
	  <div class="wrapper">
	    <div class="weatherWidget">
	      <div id="loading">Loading...</div>
	      <div class="place">
	        <span class="city"></span><span class="country"></span>
	      </div>
	      <div class="temp"></div>
	      <div class="desc"></div>
	      <img id="icon">
	    </div>
	  </div>
	</main>
	<!--<p id="license">SVGs licensed under Creative Commons. See <a href="https://www.amcharts.com/">amCharts</a>.</p> -->
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Roboto');

body {
    background: url("https://source.unsplash.com/OHzkfrv9Ycw") fixed center;  /* default background image */
    background-size: cover;
    font-family: 'Roboto', sans-serif;
    -webkit-transition: background 5s ease;
    -o-transition: background 5s ease;
    transition: background 5s ease;
    height: 100%;
}

.wrapper {
    margin: auto;
    padding: 0 20px;
    max-width: 1024px;
}

main {
    position: absolute;
    top: 40%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
        -ms-transform: translate(-50%, -50%);
            transform: translate(-50%, -50%);
    margin-top: 200px;
    margin-bottom: 100px;
}

.weatherWidget {
    text-align: center;
    font-size: 3rem;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 3%;
    min-width: 7em;
    min-height: 5em;
    padding: 1em;
    -webkit-box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
            box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
}

.temp {
    cursor: pointer;
}

#icon {
    height: 5em;
}

/*

#license {
    position: absolute;
    bottom: 0;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
        -ms-transform: translate(-50%, -50%);
            transform: translate(-50%, -50%);
    font-size: 12px;
    color: #fff;
}
*/

a {
    color: lightblue;
}

#loading { 
    position:absolute;
    left:calc(50% - 100px);
    top:calc(50% - 25px);
}
              
            
!

JS

              
                /* 
    A local weather widget that pulls data from openweathermap.org

    SVG and background image will change to reflect day/night & conditions

    C/F conversion when temp is clicked
           
*/

let isCelsius = true;
let date = new Date();
let today = date.getHours(); 

// Pull api from openweathermap.org 
const getApiUrl = (lat, lon) => 'https://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&appid=2bcc4123a99bf9b75c11673b0029ea8a';

const geoSuccess = position => {
  startPos = position;
  lat = startPos.coords.latitude;
  lon = startPos.coords.longitude;

  // Call the api and fetch the local weather
  makeRequest('GET', getApiUrl(lat, lon), handleWeather);
};

// Request server data
const makeRequest = (type, url, callback) => {
  const req = new XMLHttpRequest();
  let response = '';

  req.open(type, url, true);

  // Confirm response is ready (200: "OK")
  req.onreadystatechange = () => {
    if (req.readyState === XMLHttpRequest.DONE && req.status === 200) {
      callback(req.responseText);
    }
  }
  req.send(null);
};

const setNodeContents = (node, content) => {
  document.querySelector(node).innerHTML = content;
}

// Round temp to nearest decimal
const formatTemp = x => Number.parseFloat(x).toFixed(1);

// Fill with fetched data
const updateTempDashboard = (city, country, temp, desc) => {
  document.querySelector('#loading').remove();
  setNodeContents('.city', city);
  setNodeContents('.country', `, ${country}`);
  setNodeContents('.temp', `${temp} ºC`);
  setNodeContents('.desc', desc);
}

// ºC and ºF conversions
const getCelsius = tempKelvin => formatTemp(tempKelvin - 273);
const getFahrenheit = tempKelvin => formatTemp(1.8 * (tempKelvin - 273) + 32);

// Conversion toggle
const handleToggleTemp = () => {
  console.log('clicked');
  if(isCelsius) {
    setNodeContents('.temp', tempF + ' ºF');
  } else {
    setNodeContents('.temp', tempC + ' ºC');
  }
  isCelsius = !isCelsius;
}

// Set data variables
const handleWeather = (data) => {
  // console.log(data);
  const obj = JSON.parse(data);
  city = obj.name;
  country = obj.sys.country;
  tempKelvin = obj.main.temp;
  tempF = getFahrenheit(tempKelvin);
  tempC = getCelsius(tempKelvin);
  tempDescription = obj.weather[0].main;
  
  document.querySelector('.temp').addEventListener('click', handleToggleTemp);

  updateTempDashboard(city, country, tempC, tempDescription);

  // Select background image and icon based on tempDescription & current time
  let image = document.getElementById("icon");

  if (tempDescription === "Rain") {
    if (today >= 7 && today <= 19) {
      document.body.style.background = "url('https://source.unsplash.com/b-sTYb0quYQ') no-repeat center center fixed";
      document.body.style.backgroundSize = "cover";
      image.src = "https://res.cloudinary.com/trobes/image/upload/v1537821722/rainy-1.svg"; 
    }
    else {
      document.body.style.background = "url('https://source.unsplash.com/LM4X_cIjjQ8') no-repeat center center fixed";
      document.body.style.backgroundSize = "cover";
      image.src = "https://res.cloudinary.com/trobes/image/upload/v1537821730/rainy-7.svg"; 
    }

  } else if (tempDescription === "Thunderstorm") {
      if (today >= 7 && today <= 19) {
        document.body.style.background = "url('https://source.unsplash.com/2uOcrLACf_4') no-repeat center center fixed";
        document.body.style.backgroundSize = "cover";
        image.src = "https://res.cloudinary.com/trobes/image/upload/v1537821752/thunder.svg"; 
      }
      else {
        document.body.style.background = "url('https://source.unsplash.com/vP5Im4q8Z6g') no-repeat center center fixed";
      }

  } else if (tempDescription === "Snow") {
      if (today >= 7 && today <= 19) {
        document.body.style.background = "url('https://source.unsplash.com/pY1FKeEP8v8') no-repeat center center fixed";
        document.body.style.backgroundSize = "cover";
        image.src = "https://res.cloudinary.com/trobes/image/upload/v1537821749/snowy-3.svg"; 
      }
      else {
        document.body.style.background = "url('https://source.unsplash.com/73osnYZ133o') no-repeat center center fixed";
        document.body.style.backgroundSize = "cover";
        image.src = "https://res.cloudinary.com/trobes/image/upload/v1537821752/snowy-6.svg"; 
      }

  } else if (tempDescription === "Clear") {
      if (today >= 7 && today <= 19) {
        document.body.style.background = "url('https://source.unsplash.com/E9aetBe2w40') no-repeat center center fixed";
        document.body.style.backgroundSize = "cover";
        image.src = "https://res.cloudinary.com/trobes/image/upload/v1537821710/day.svg"; 
      }
      else {
        document.body.style.background = "url('https://source.unsplash.com/UiO6KzrBTJk') no-repeat center center fixed";
        document.body.style.backgroundSize = "cover";
        image.src = "https://res.cloudinary.com/trobes/image/upload/v1537821712/night.svg"; 
      }

  } else if (tempDescription === "Clouds") {
      if (today >= 7 && today <= 19) {
        document.body.style.background = "url('https://source.unsplash.com/3Eqc3Ph4oRg') no-repeat center center fixed";
        document.body.style.backgroundSize = "cover";
        image.src = "https://res.cloudinary.com/trobes/image/upload/v1537821701/cloudy-day-2.svg"; 
      }
      else {
        document.body.style.background = "url('https://source.unsplash.com/B31OD0bsYV0') no-repeat center center fixed";
        document.body.style.backgroundSize = "cover";
        image.src = "https://res.cloudinary.com/trobes/image/upload/v1537821702/cloudy-night-2.svg"; 
      }

  } else 
    document.body.style.background = "url('https://source.unsplash.com/3Eqc3Ph4oRg') no-repeat center center fixed";
    document.body.style.backgroundSize = "cover";
  }


// Check for Geolocation support
if (navigator.geolocation) {
  window.onload = () => {

    navigator.geolocation.getCurrentPosition(geoSuccess);

  };

} else { 
    alert('Geolocation is not supported! Check browser/os settings.'); 
}

              
            
!
999px

Console