css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container">
    <div class="panel panel-transparent text-center">
      <div class="panel-body">
      <h1 class="large-title">
        <a href="#" id="toggle-temp">
          <span id="temp">0</span> &deg;<span id="temp-symbol">F</span>
        </a>
      </h1>
      <h2 id="location">Searching for human location...</h2>
      <p id="weather-description"></p>
      </div>
  </div>
</div>
<footer class="footer">
  <div class="container">
    <div class="panel panel-transparent">
      <div class="panel-body">
    <small>Written and coded by <a href="https://www.freecodecamp.com/daon" target="_blank">Dan Andersson</a>.</small>
        <small class="pull-right" >Background image by <a id="bg-author" href="#" target="_blank">Unknown Author</a>.
      </div>
    </div>
  </div>
</footer>
            
          
!
            
              body {
  background-image: url('https://images.unsplash.com/photo-1464495310703-83aac94583ee');
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  padding-top: 50px;
}

.large-title {
  font-size: 120px;
}

.panel-transparent {
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
}

.footer {
  color: #fff;
}

.background-01d {
  background-image: url('https://images.unsplash.com/photo-1464495310703-83aac94583ee');
}

.background-01n {
  background-image: url('https://images.unsplash.com/photo-1466853817435-05b43fe45b39');
}

.background-02d {
  background-image: url('https://images.unsplash.com/photo-1455735459330-969b65c65b1c');
}

.background-02n {
  background-image: url('https://images.unsplash.com/photo-1465800872432-a98681fc5828');
}

.background-03d {
  background-image: url('https://images.unsplash.com/photo-1470850111035-2ddd4d89f671');
}

.background-03n {
  background-image: url('https://images.unsplash.com/photo-1473207730345-6cb17f27bd46');
}

.background-04d {
  background-image: url('https://images.unsplash.com/photo-1470176519524-3c2f481c8c9c');
}

.background-04n {
  background-image: url('https://images.unsplash.com/photo-1469434438443-45d782c09d4b');
}

.background-09d {
  background-image: url('https://images.unsplash.com/photo-1446053030967-d49c7b13a02c');
}

.background-09n {
  background-image: url('https://images.unsplash.com/photo-1468476396571-4d6f2a427ee7');
}

.background-10d {
  background-image: url('https://images.unsplash.com/photo-1457463968646-bbb6184dfedb');
}

.background-10n {
  background-image: url('https://images.unsplash.com/photo-1474168442801-2342fbafe264');
}

.background-11d {
  background-image: url('https://images.unsplash.com/photo-1442213391790-7656f6e368b9');
}

.background-11n {
  background-image: url('https://images.unsplash.com/photo-1465799522714-8eb0e6fccf73');
}

.background-13d {
  background-image: url('https://images.unsplash.com/photo-1446463969211-28bf6e20b315');
}

.background-13n {
  background-image: url('https://images.unsplash.com/photo-1460899162311-d63278c9cf9d');
}

.background-50d {
  background-image: url('https://images.unsplash.com/10/hazy-trees.jpg');
}

.background-50n {
  background-image: url('https://images.unsplash.com/photo-1461696114087-397271a7aedc');
}
            
          
!
            
              var lw = (function($) {
  var _authors = {
    '01d': {
      name: 'Nicholas Kampouris',
      url: 'https://unsplash.com/@nicholaskampouris'
    },
    '01n': {
      name: 'Štefan Štefančík',
      url: 'https://unsplash.com/@cikstefan'
    },
    '02d': {
      name: 'Gabriela Parra',
      url: 'https://unsplash.com/@gabriela_parra'
    },
    '02n': {
      name: 'Layne Lawson',
      url: 'https://unsplash.com/@laynelawson'
    },
    '03d': {
      name: 'Tobias van Schneider',
      url: 'https://unsplash.com/@vanschneider'
    },
    '03n': {
      name: 'Chris Coudron',
      url: 'https://unsplash.com/@coudron'
    },
    '04d': {
      name: 'Ivan Vranić',
      url: 'https://unsplash.com/@hvranic'
    },
    '04n': {
      name: 'Mike Wilson',
      url: 'https://unsplash.com/@mkwlsn'
    },
    '09d': {
      name: 'Bagus Ghufron',
      url: 'https://unsplash.com/@bagusghufron'
    },
    '09n': {
      name: 'Todd Diemer',
      url: 'https://unsplash.com/@todd_diemer'
    },
    '10d': {
      name: 'Lauren Ellis',
      url: 'https://unsplash.com/@lakael'
    },
    '10n': {
      name: 'zhang dayong',
      url: 'https://unsplash.com/@dayongzhang'
    },
    '11d': {
      name: 'Lucy Chian',
      url: 'https://unsplash.com/@shlucy'
    },
    '11n': {
      name: 'Layne Lawson',
      url: 'https://unsplash.com/@laynelawson'
    },
    '13d': {
      name: 'David Creixell Mediante',
      url: 'https://unsplash.com/@kingklaudi'
    },
    '13n': {
      name: 'Sam X',
      url: 'https://unsplash.com/@samx'
    },
    '50d': {
      name: 'Kundan Ramisetti',
      url: 'https://unsplash.com/@kundanramisetti'
    },
    '50n': {
      name: 'Anders Jildén',
      url: 'https://unsplash.com/@andersjilden'
    }
  };
  var _temp = 0;
  var _tempElement = $('#temp');
  var _tempSymbolElement = $('#temp-symbol');
  var _locationElement = $('#location');
  
  $('#toggle-temp').on('click', _toggleTemp);
  
  function _toCelcius(kelvin) {
    return (kelvin - 273.15).toFixed(1);
  }
  
  function _toFahrenheit(kelvin) {
    return (kelvin * 9/5 - 459.67).toFixed(1);
  }
  
  function _toggleTemp() {
    if (_tempSymbolElement.html() === 'C') {
      _tempSymbolElement.html('F');
      _tempElement.html(_toFahrenheit(_temp));
    } else {
      _tempSymbolElement.html('C');
      _tempElement.html(_toCelcius(_temp));
    }
  }
  
  function _getApiUrl(lat, lon) {
    var apiUrl = 'http://api.openweathermap.org/data/2.5/weather';
    apiUrl += '?lat=' + lat;
    apiUrl += '&lon=' + lon;
    apiUrl += '&APPID=4fb0fad19392f981178a21ebbb90cfd7';
    return apiUrl;
  }
  
  function _geolocationIsAvailable() {
    return "geolocation" in navigator && window.location.protocol === "https:";
  }
  
  function _getCurrentPosition(callback) {  
    var cache;
    
    function getCurrentPositionFallback() {
      if (cache) callback(cache);  
      $.getJSON('http://freegeoip.net/json/', function(result) {
        cache = {
          coords: {
            "latitude": result.latitude, 
            "longitude": result.longitude
          }
        };
        callback(cache);
      })
      .fail(function(error) {
        _locationElement.html('Sorry, human location not found. We so sad.')
      });
      
    }  
    if (_geolocationIsAvailable()) {
      navigator.geolocation.getCurrentPosition(callback, getCurrentPositionFallback);
    } else {
      getCurrentPositionFallback();
    }
  }
  
  function _parseLocalWeather(localWeather) {
    var weather = localWeather.weather[0];
    $('body').addClass('background-' + weather.icon);
    var author = _authors[weather.icon];
    if (author) {
      $('#bg-author')
        .attr('href', author.url)
        .html(author.name);
    }
    _temp = localWeather.main.temp;
    _tempElement.html(_toFahrenheit(_temp));
    _locationElement.html(localWeather.name + ', ' + localWeather.sys.country);
    $('#weather-description').html(weather.description)
  };
  
  function getLocalWeather() {
    _getCurrentPosition(function(position) {
      if (position.coords) {
        $.getJSON(_getApiUrl(position.coords.latitude, position.coords.longitude), function(response)  {
          _parseLocalWeather(response);
        });
      }
    });
  }
  
  return {
    getLocalWeather: getLocalWeather
  };

})(jQuery);

$(function() {
  lw.getLocalWeather();
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console