cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <div class="weather-container">
        <div class="weather">getting location</div>
        <div class="loader">
          <div class="left"></div>
          <div class="right"></div>
        </div>
      </div>
            
          
!
            
              html {
  height: 100%;
}

body {
  background-color: rgba(245, 245, 245, 0.6);
  font-family: 'Josefin Sans', sans-serif;
  font-size: medium;
  color: rgb(52, 52, 52);
  height: 100%;
  margin: 0px;
  padding: 0px;
  text-align: center;
  background-image: url('https://hd.unsplash.com/photo-1414236062502-90ed8c981d71');
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center;
  background-size: cover;
}

.weather-container {
  height: 40%;
  width: 50%;
  margin: 10% 0% 0% 25%;
  background-color: rgba(3, 169, 244, 0.48);
  border-radius: 2px 2px 2px 2px;
  border: rgba(3, 169, 244, 0.2) solid 0.2em;
  -moz-user-select: none;
  -webkit-user-select: none;
  color: rgba(211, 219, 226, 0.91);
  position: absolute;
}

.weather {
  padding: 1em;
  vertical-align: middle;
  font-size: 2.2em;
  text-shadow: rgba(27, 26, 26, 0.75) 0px 0px 5px;
}

.unit {
  cursor: pointer;
}

.colored-name {
  color: #E74C3C;
  font-weight: bold;
}

.left {
  background-color: rgba(95, 55, 167, 0.61);
}

.right {
  background-color: rgba(232, 106, 96, 0.78);
}

.right,
.left {
  width: 3em;
  height: 3em;
  position: absolute;
  margin-left: 45%;
  border-top: 1px solid #00bcd4;
  border-radius: 50%;
  animation-duration: 1600ms;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}

.left {
  animation-name: spin-right;
}

@keyframes spin-right {
  0% {
    transform: rotateZ(0deg);
  }
  25% {
    transform: rotateZ(90deg) translateY(1em);
  }
  50% {
    transform: rotateZ(180deg);
  }
  75% {
    transform: rotateZ(270deg) translateY(1em);
  }
  100% {
    transform: rotateZ(360deg);
  }
}

.right {
  animation-name: spin-left;
}

@keyframes spin-left {
  0% {
    transform: rotateZ(0deg);
  }
  25% {
    transform: rotateZ(90deg) translateY(-1em);
  }
  50% {
    transform: rotateZ(180deg);
  }
  75% {
    transform: rotateZ(270deg) translateY(-1em);
  }
  100% {
    transform: rotateZ(360deg);
  }
}
            
          
!
            
              // Set up global variables
var currentWeather = {};
var userLocation = {
  "latitude": 52,
  "longitude": 13
};

// Start the engines
$(document).ready(function() {
  // Load first quote
  getuserLocation();
});

function setUpToggler() {
  // Set up unit toggler
  $(".unit").on("click", function() {
    console.log("toggle unit click received");
    toggleUnit();
    setWeather();
  });
}

function getuserLocation() {
  // get userLocation from browser
  if ("geolocation" in navigator) {
    navigator.geolocation.getCurrentPosition(function(position) {
      userLocation.latitude = position.coords.latitude;
      userLocation.longitude = position.coords.longitude;
      getWeather(userLocation);
    });
  } else {
    setWeather("You seem to be in no man's land", "or Mars?")
  }
}

function getWeather(userLocation) {
  $(".weather").html("getting weather info").fadeIn();
  var loc = "lat=" + userLocation.latitude + "&lon=" + userLocation.longitude;
  var url = "https://crossorigin.me/http://api.openweathermap.org/data/2.5/weather?" + loc + "&appid=a9f058ad5a356fda1be2f552967e668d";
  $.get(url, parseWeather);
}

function parseWeather(data) {
  fullWeatherResponse = data;
  console.log("full response", data);
  // Save weather info and set it afterwards
  currentWeather = {
    "info": {
      "desc": data.weather[0].main,
      "temp": parseInt(data.main.temp - 273.15),
      "unit": "&#8451;"
    },
    "loc": {
      "city": data.name,
      "country": data.sys.country
    }
  };
  setWeather();
}

function toggleUnit() {
  console.log("toggling units");
  var temp = currentWeather.info.temp;
  var unit = currentWeather.info.unit;
  if (unit == "&#8451;") {
    temp = parseInt((temp * 1.8) + 32);
    unit = "&#8457;";
  } else {
    temp = parseInt((temp - 32) / 1.8);
    unit = "&#8451;";
  }
  currentWeather.info.temp = temp;
  currentWeather.info.unit = unit;
}

function setWeather(err) {
  $(".loader").hide();
  if (err) {
    $(".weather").html(err).fadeIn();
  }
  // Set quote and author in visually appealing way
  else {
    //Show the right background
    if (weatherbackgrounds[currentWeather.info.desc.toLowerCase()]) {
      $("body").css("background-image", weatherbackgrounds[currentWeather.info.desc.toLowerCase()]);
    } else {
      $("body").css("background-image", weatherbackgrounds.default);
    }

    // Write out the temperature
    var weatherLine = "<p>" + currentWeather.info.temp + '<span class="unit">' + currentWeather.info.unit + "</span><br>"+ currentWeather.info.desc +" in " + currentWeather.loc.city + ", " + currentWeather.loc.country + "</p>";
    $(".weather").html(weatherLine).fadeIn();
    setUpToggler();
  }
}

var weatherbackgrounds = {
  "snow": "url('https://hd.unsplash.com/photo-1446463969211-28bf6e20b315')",
  "drizzle": "url('https://hd.unsplash.com/uploads/14116603688211a68546c/30f8f30b')",
  "rain": "url('https://hd.unsplash.com/uploads/14116603688211a68546c/30f8f30b')",
  "clear": "url('https://hd.unsplash.com/photo-1415695989345-dcf757ebb173')",
  "clouds": "url('https://hd.unsplash.com/photo-1472108450932-9afc984efb4c')",
  "mist": "url('https://hd.unsplash.com/photo-1445294211564-3ca59d999abd')",
  "extreme": "url('https://hd.unsplash.com/photo-1431440869543-efaf3388c585')",
  "default": "url('https://hd.unsplash.com/photo-1441749074938-1c6782303919')"
};
            
          
!
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