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

              
                .valign-wrapper.full
  .container.glow
    h1.light-blue-text.text-lighten-2.center-align Weather
    .row
      .col.m6.offset-m3.s12
        .center-align.blue-text.text-lighten-4.mt-10
          .row
            .col.s12.m6
              p#temperature.main-text 00 ºC
            .col.s12.m6
              i#mainwicon.material-icons.main-icon
          .mt-10
            p#state.flow-text ...
          .mt-10
            p#loc.flow-text Earth
.footer
  .row
    .col-md-12.center-align
      p.blue-text.text-lighten-4 Made with 🍫 by 
        a(href="https://mrcrjs.github.io") Marco A. Rojas
              
            
!

CSS

              
                html
  height 90%
h1
  font-family Poiret One
body
  background-color #37474f
  height: 100%
.full
  height: 100%
.glow
  text-shadow 0 0 6px
.mt-10
  margin-top 2rem
  
.main-text
  padding-top 2rem
  font-size 4rem
.main-icon
  font-size 7rem
              
            
!

JS

              
                jQuery.ajaxPrefilter((options) => {
    if (options.crossDomain && jQuery.support.cors)
    {
        options.url = 'https://cors-anywhere.herokuapp.com/' + options.url;
    }
});

const tryGeolocation = () => {
  if (navigator.geolocation)
  {
    navigator.geolocation.getCurrentPosition(
      getWeather,
      browserGeolocationFail, {
        maximumAge: 50000,
        timeout: 20000,
        enableHighAccuracy: true,
      }
    );
  }
};

const browserGeolocationFail = error => {
  switch (error.code) {
    case error.TIMEOUT:
      console.log(`Browser geolocation error!\n\nTimeout.`);
      tryGeolocation();
      break;
    case error.PERMISSION_DENIED:
      if (error.message.indexOf(`Only secure origins are allowed`) === 0)
      {
        tryAPIGeolocation();
      }
      break;
    case error.POSITION_UNAVAILABLE:
      console.log(`Browser geolocation error!\n\nPosition unavailable.`);
      break;
  }
};

const tryAPIGeolocation = () => {
  $.post(`https://www.googleapis.com/geolocation/v1/geolocate?key=${googleKey}`, (data) => {
      getWeather({
        coords: {
          latitude: data.location.lat,
          longitude: data.location.lng,
        }
      });
    })
    .fail((err) => {
      console.log(`Geolocation API error!\n${err}`);
    });
};

const getWeather = position => {
  const lat = position.coords.latitude;
  const lon = position.coords.longitude;
  const weatherUrl = `http://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${weatherKey}&units=metric`;
  $.getJSON({
    type: 'GET',
    url: weatherUrl,
    crossDomain: true,
  }, setWeather);
}

const setWeather = data => {
  const country = data.sys.country;
  const city = data.name;
  const weCod = data.weather[0].id;
  const temp = parseInt(data.main.temp);
  let state = data.weather[0].description;
  state = capitalize(state);
  $("#loc").text(`${city}, ${country}`);
  $("#mainwicon").addClass(`wi wi-owm-${weCod}`);
  $("#state").html(`${state}`);
  $("#temperature").html(`${temp} ºC`);
}

const toggleDegrees = () => {
  const str = $("#temperature").text();
  const degrees = str.charAt(str.length-1);
  let temp = str.substr(0,str.length-2);
  if(degrees=="C")
  {
    temp = decRound(ceToFa(temp));
    $("#temperature").text(`${temp} ºF`);
  }
  if(degrees=="F")
  {
    temp = decRound(faToCe(temp));
    $("#temperature").text(`${temp} ºC`);
  }
}
  
const decRound = number => Math.round( number * 10 ) / 10;

const ceToFa = celsius => (celsius * 1.8) + 32;

const faToCe = fahrenheit => (fahrenheit - 32)/1.8;  

const capitalize = string => string.charAt(0).toUpperCase() + string.slice(1);

$(() => {
  tryGeolocation();
  $("#temperature").on("click", toggleDegrees);
});

// Google GeoLocation API Key:
const googleKey = `AIzaSyB6e6H8dk1KI4FGyRMfvHCKCV4OnnzTtHs`
// Open Weather API Key: 
const weatherKey = `9362ccf7726e553cac58d1894b27ff3a`
              
            
!
999px

Console