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 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

              
                <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">

<section class="card" data-weather="card">
  <header class="heading">
    <h1 class="title">Weather App</h1>
    <h3 class="sub-title">freeCodeCamp</h3>
  </header>
  <div class="body">
   <div class="temperature">
     <figure class="icon">
         <img src="" alt="" data-weather="icon" />
     </figure>
     <div class="content" data-weather="temperature">
       <div class="is-fahrenheit is-hidden">
         <span class="value" data-weather="fahrenheit"></span>
         <span>F</span>
       </div>
       <div class="is-celsius">   
         <span class="value" data-weather="celsius"></span>
         <span class="scale">C</span>
       </div>
     </div>
     <div class="actions">
       <button class="button is-active" data-weather-action="show-celcius">Celcius</button>
       <button class="button" data-weather-action="show-fahrenheit">Fahrenheit</button>
     </div>
   </div>
  </div>
  <footer class="footer">
    <div class="weather">
      <h3 class="city" data-weather="local">...</h3>
      <p class="clime" data-weather="clime">...</p>
    </div>
  </footer>
</section><!-- ./card -->

<footer class="copywrite">
  <p class="copywrite">By <a href="https://alexsandro-sa.com.br/" target="_blank">Alexsandro S. Alves</a>.</p>
  <p>Icons by <a href="https://www.iconfinder.com/iconsets/weather-color-2" target="_blank">Yun Liu(Iconfinder)</a></p>
</footer>
              
            
!

CSS

              
                // Variables
// ––––––––––––––––––––––––––––––––––––––––––––––––––
$body-bg: #f1f1f1;
$gray: #ccc;
$text-color: #78868A;

// Reset
// ––––––––––––––––––––––––––––––––––––––––––––––––––
*, *:before, *:after {
  box-sizing: border-box;
}

a, button {
  cursor: pointer;
}

// Animations
// ––––––––––––––––––––––––––––––––––––––––––––––––––
@keyframes pulse {
  0% {
    transform: scale(0.8);
  }
  50% {
    transform: scale(1);
  }
  100% {
    transform: scale(0.8);
  }
}

// Generic
// ––––––––––––––––––––––––––––––––––––––––––––––––––
body {
  background-color: #F0F1F5;
  font-family: 'Roboto', sans-serif;
}

.button {
  display: inline-block;
  font-weight: normal;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  user-select: none;
  border: 1px solid transparent;
  padding: 0.25rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1.5;
  transition: all 0.2s ease-in-out;
  
  &.is-active {
    background-color: #5DB371;
    color: #fff;
  }
}

// Util
// ––––––––––––––––––––––––––––––––––––––––––––––––––
.is-hidden {
  display: none;
}


// Components
// ––––––––––––––––––––––––––––––––––––––––––––––––––
.card {
  position: relative;
  width: 350px;
  height: 550px;
  margin-top: 50px;
  margin-left: auto;
  margin-right: auto;
  padding: .5em;
  
  background-color: #fff;
  border-radius:10px;
  box-shadow: -1px 6px 17px -1px rgba(153,153,153,1);
  
  > .heading {
    text-align: center;
    
    > .title {
      margin-bottom: 5px;
      font-size: 2em;
    }
    
    > .sub-title {
      margin-top: 0;
      font-size: .8em;
      text-transform: uppercase;
      color: $text-color;
    }
  }
  
  > .body {
    min-height: 100%;
  }
  
  > .footer {
    position: absolute;
    bottom: 0;
    min-height: 105px;
    width: 100%;
    margin-left: -8px;
  }
}

.temperature {
  font-size: 2.5em;
  text-align: center;
  
  > .icon {
    margin-top: 5px;
    margin-bottom: 5px;
    animation: pulse 4s infinite;    
  }
  
  > .content {
    position: relative;
    letter-spacing: -5px;
    
    .value {
      position: relative;
     
      &:after {
        content: 'º';
        padding-left: 5px;
      }
    }
  }
  
  > .actions {
    clear: both;
    width: 200px;
    margin-left: auto;
    margin-right: auto;
    margin-top: 5px;
    
    > .button {
      float:left;
      margin: 0;
      width:100px;
    }
  }
}

.weather {
  text-align: center;
  color: $text-color;
  
  .title {
    margin-bottom: 0;    
  }
}

.copywrite {
  font-size: 10px;
  text-align: center;
  
  a {
    color: #000;
    font-weight: bold;
    text-decoration: none;
    cursor: pointer;
  }
}

              
            
!

JS

              
                function renderWeatherByLocation() {
  // const apiKey = 'a1f9dbe69aa1cf96c8dec4633c032ca5';
  
  $.when(getCoordByCurrentLocation())
  .then((location) => {
     $.ajax({
      url: 'https://fcc-weather-api.glitch.me/api/current',
      method: 'GET',
      data: {
        lat: location.latitude,
        lon: location.longitude
      }
    })
    .done((response) => showDataWeather(response))
    .fail((err) => showFeedback(err));
  });
}

function getCoordByCurrentLocation() {
  return $.ajax({
    url: 'https://ipapi.co/json/'
  });
}

function showFeedback(response) {
  console.log("error");
}

function showDataWeather(data) {
  const local = data.name +', '+ data.sys.country;
  const clime = data.weather[0].main;
  const icon = getIcon(clime);
  const fahrenheit = Math.round((data.main.temp * 9)/5 + 32);
  const celsius = Math.round(data.main.temp);
  
  $('[data-weather="icon"]').attr('src', icon);
  $('[data-weather="local"]').text(local);
  $('[data-weather="clime"]').text(clime);
  $('[data-weather="celsius"]').text(celsius);
  $('[data-weather="fahrenheit"]').text(fahrenheit);
}

function getIcon(name) {
  let clime = name.toLowerCase();
  return (isClimeValid(clime)) ? getIconByClime(clime) : null;
}

function isClimeValid(clime) {
  const climes = climeOptions()
  return !!$.inArray(clime, climes);
}

function climeOptions() {
  return [
    'drizzle','clouds', 'rain', 'snow', 'clear', 'thunderstom'
  ];
}

function getIconByClime(clime) {
  const iconClimeOptions = getIconsClime();
  return iconClimeOptions[clime];
}

function getIconsClime() {
  return {
    'drizzle': 'https://cdn2.iconfinder.com/data/icons/weather-color-2/500/weather-30-256.png',
    'clouds': 'https://cdn2.iconfinder.com/data/icons/weather-color-2/500/weather-22-256.png', 
    'rain': 'https://cdn2.iconfinder.com/data/icons/weather-color-2/500/weather-32-256.png', 
    'snow': 'https://cdn2.iconfinder.com/data/icons/weather-color-2/500/weather-24-256.png',   
    'clear': 'https://cdn2.iconfinder.com/data/icons/weather-color-2/500/weather-01-256.png', 
    'thunderstom': 'https://cdn2.iconfinder.com/data/icons/weather-color-2/500/weather-23-256.png'
  };
}

renderWeatherByLocation();

// Events
// ––––––––––––––––––––––––––––––––––––––––––––––––––

var $doc = $(document);
$doc.on('click', '[data-weather-action="show-celcius"]', change);
$doc.on('click', '[data-weather-action="show-fahrenheit"]', change);

function change(){
  const $el = $(this);
  if($el.hasClass('is-active')) return;
  
  const $actionCelcius = $('[data-weather-action="show-celcius"]');
  const $tempCelcius = $('.is-celsius');
  const $action$tempFahrenheit = $('[data-weather-action="show-fahrenheit"]');
  const $tempFahrenheit = $('.is-fahrenheit');
  
  if($el.data('weatherAction') == 'show-fahrenheit') {
    $tempCelcius.addClass('is-hidden');
    $actionCelcius.removeClass('is-active');
    
    $tempFahrenheit.removeClass('is-hidden');
    $action$tempFahrenheit.addClass('is-active');
  }
  else {
    $tempFahrenheit.addClass('is-hidden');
    $action$tempFahrenheit.removeClass('is-active');
    
    $tempCelcius.removeClass('is-hidden');
    $actionCelcius.addClass('is-active');
  }
}
              
            
!
999px

Console