123

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="bg">
  <img id="bg" src="" style="width: 100%; position: fixed; left: 0%">
  <div class="header col-xs-6" style="text-align: center">
    <h1><br><br>Welcome to the Weather  Page!</h1>
  </div>

  <div class="weather-box">
    <div class="container">
      <div class="row">
        <div class="col-xs-12 col-sm-6 loc"></div>
      </div>
      <div class="row">
        <div class="col-xs-2" style="padding-top:  20px; text-align: right" id="icons">
          <i id="main-icon" class="wi"></i><br>
        </div>
        <div class="col-xs-8">
          <h2 id="main"></h2>
        </div>
      </div>
      <div class="row"> 
        <div class="col-xs-2" style="padding-top:  20px; text-align: right" id="icons">
        <i id="temp-icon" class="wi"></i>
      </div>
      <div class="col-xs-8">
        <h2 id="temp">Temp: </h2>
      </div>
    </div>
    <div class="row">
      <div class="col-xs-2" style="padding-top:  20px; text-align: right" id="icons">
        <i id="wind-icon" class="wi"></i><br>
      </div>
      <div class="col-xs-8">
        <h2 id="wind">Wind: </h2>
      </div>
    </div>
    <div class="row">

      <div class="col-xs-2" style="padding-top:  20px; text-align: right" id="icons">
        <i id="humid-icon" class="wi"></i><br>
      </div>
      <div class="col-xs-8">
        <h2 id="humid">Humidity: </h2>
      </div>

    </div>
    <div class="row">

      <div class="col-xs-2" style="padding-top:  20px; text-align: right" id="icons">
        <i id="sunr-icon" class="wi"></i><br>
      </div>
      <div class="col-xs-8">
        <h2 id="sunrise">Sunrise: </h2>

      </div>
    </div>
    <div class="row">

      <div class="col-xs-2" style="padding-top:  20px; text-align: right" id="icons">
        <i id="suns-icon" class="wi"></i><br>
      </div>
      <div class="col-xs-8">
        <h2 id="sunset">Sunset: </h2>

      </div>
    </div>
  </div>
  <!--       <div class="col-xs-4" style="text-align: right;">
        <h2 id="long"></h2></div> -->
</div>
</div>
</div>
            
          
!
            
              div.bg {
  background-color: black;
  width: 100vw; 
  height: 100vh
}

.loc {
  text-align: center;
}

h1 {
  color: red;
  font-family: Lobster, "Brush Script MT", cursive;
}

.header {
  position: fixed;
  top: 0%;
  left: 25%;
  z-index: 10;
}

.weather-box {
  position: fixed;
  top: 200px;
  left: 25%;
  right: 25%;
  padding: 15px;
  border: none;
  background-color: rgba(0,0,0,0.75);
  z-index: 1;
}

.wi {
  font-size: 19pt;
  color: white;
}

h2 {
  color: white;
  font-size: 16pt;
}

.converter {
  background-color: transparent;
  border: none;
  focus: none;
  outline: none;
  z-index: 100;
}
            
          
!
            
              $(document).ready(function() {
  metricWeather();
});

function metricWeather() {
  var ipApi = 'http://ip-api.com/json?callback=?';
  $.getJSON(ipApi, (data) => {
    $(".loc").html("<h2>The weather in beautiful " + data.city + " is:</h2><br>");
    var wApi = 'http://api.openweathermap.org/data/2.5/weather?q=' + data.city + '&units=metric&appid=cd108db1bd9e703fd35a57ba25d64c83';
    $.getJSON(wApi, (json) => {
      var sunrise = dateCalc(json.sys.sunrise);
      var sunset = dateCalc(json.sys.sunset);
      var windDir = heading(json.wind.deg);
      $("#main").html("Main: " + json.weather[0].main);
      $("#temp").html('Temp: ' + Math.round(json.main.temp) + "<button id=\"CtoF\" class=\"btn btn-primary converter\"><i class=\"wi wi-celsius\"></i></button>");
      $("#wind").html('Wind: ' + Math.round(json.wind.speed * 36)/10 + " kph " + windDir );
      $("#humid").html('Humiditity: ' + json.main.humidity);
      $("#sunrise").html('Sunrise: ' + sunrise);
      $("#sunset").html('Sunset: ' + sunset);
      $("#CtoF").on('click', function() {
        imperialWeather();
      });
      var iconStr = iconSelector(json, windDir);
      $("#main-icon").html("<i class='wi " + iconStr.main + "'></i>");
      $("#temp-icon").html("<i class='wi " + iconStr.temp + "'></i>");
      $("#wind-icon").html("<i class='wi " + iconStr.wind + "'></i>");
      $("#humid-icon").html("<i class='wi " + iconStr.humid + "'></i>");
      $("#sunr-icon").html("<i class='wi " + iconStr.sunr + "'></i>");
      $("#suns-icon").html("<i class='wi " + iconStr.suns + "'></i>");
      $("#bg").attr("src", iconStr.img);
    });
  });
}

function imperialWeather() {
  var ipApi = 'http://ip-api.com/json?callback=?';
  $.getJSON(ipApi, function(data) {
    var wApi = 'http://api.openweathermap.org/data/2.5/weather?q=' + data.city + '&units=imperial&appid=cd108db1bd9e703fd35a57ba25d64c83';
    $.getJSON(wApi, function(json) {
      var sunrise = dateCalc(json.sys.sunrise);
      var sunset = dateCalc(json.sys.sunset);
      var windDir = heading(json.wind.deg);
      $("#main").html("Main: " + json.weather[0].main);
      $("#temp").html('Temp: ' + Math.round(json.main.temp) + "<button id=\"FtoC\" class=\"btn btn-primary converter\"><i class=\"wi wi-fahrenheit\"></i></button>");
      $("#wind").html('Wind: ' + Math.round(json.wind.speed*10)/10 + " mph " + windDir );
      $("#humid").html('Humiditity: ' + json.main.humidity);
      $("#sunrise").html('Sunrise: ' + sunrise);
      $("#sunset").html('Sunset: ' + sunset);
      $("#FtoC").on('click', function() {
        metricWeather();
      });
      var iconStr = iconSelector(json, windDir);
      $("#main-icon").addClass(iconStr.main );
      $("#temp-icon").addClass(iconStr.temp );
      $("#wind-icon").addClass(iconStr.wind );
      $("#humid-icon").addClass(iconStr.humid);
      $("#sunr-icon").addClass(iconStr.sunr);
      $("#suns-icon").addClass(iconStr.suns);
      $("#bg").attr("src", iconStr.img);
    });
  })

  ;
}

function dateCalc(time) {
  // Create a new JavaScript Date object based on the timestamp
  // multiplied by 1000 so that the argument is in milliseconds, not seconds.
  var date = new Date(time * 1000);
  // Hours part from the timestamp
  var hours = date.getHours();
  // Minutes part from the timestamp
  var minutes = "0" + date.getMinutes();
  // Seconds part from the timestamp
  var seconds = "0" + date.getSeconds();

  // Will display time in 10:30:23 format
  return hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
}

function heading(dir) {
  if (dir >= 0 && dir <= 11) {
    return "N";
  } else if (dir > 11 && dir <= 33) {
    return "N-NE";
  } else if (dir > 33 && dir <= 56) {
    return "NNE";
  } else if (dir > 56 && dir <= 78) {
    return "E-NE";
  } else if (dir > 78 && dir <= 101) {
    return "E";
  } else if (dir > 101 && dir <= 123) {
    return "E-SE";
  } else if (dir > 123 && dir <= 146) {
    return "SE";
  } else if (dir > 146 && dir <= 168) {
    return "S-SE";
  } else if (dir > 168 && dir <= 191) {
    return "S";
  } else if (dir > 191 && dir <= 213) {
    return "S-SW";
  } else if (dir > 213 && dir <= 236) {
    return "SW";
  } else if (dir > 236 && dir <= 258) {
    return "W-SW";
  } else if (dir > 258 && dir <= 281) {
    return "W";
  } else if (dir > 281 && dir <= 303) {
    return "W-NW";
  } else if (dir > 303 && dir <= 326) {
    return "NW";
  } else if (dir > 326 && dir <= 348) {
    return "N-NW";
  } else if (dir > 348 && dir <= 360) {
    return "N";
  }
}

function iconSelector(json, windDir) {
  var icons = {
    main: "",
    temp: "",
    wind: "",
    humid: "wi-humidity",
    sunr: "wi-sunrise",
    suns: "wi-sunset",
    img: ""
  };
  switch (json.weather[0].main) {
    case "Mist":
      icons.main = "wi-fog";
      icons.img = "http://www.borongaja.com/data_images/out/17/636123-morning-mist.jpg";
      break;
    case "Clear":
      icons.main = "wi-day-sunny";
      icons.img = "http://science-all.com/images/wallpapers/sunny-pic/sunny-pic-7.jpg";
      break;
    case "Clouds":
      icons.main = "wi-cloudy";
      icons.img = "http://hayatimagazine.com/wp-content/uploads/2015/04/tree-under-cloudy-sky-nature-hd-wallpaper-1920x1200-3821.jpg";
      break;
    case "Fog":
      icons.main = "wi-fog";
      icons.img = "http://kingofwallpapers.com/fog/fog-008.jpg";
      break;
    case "Sunny":
      icons.main = "wi-day-sunny";
      icons.img = "https://1.bp.blogspot.com/-XoXOi1GZgGA/T-XnITthnKI/AAAAAAAAAD4/hp2JRzOG4-w/s1600/29-Deserted+Beach+with+a+Sunny+Weather.jpg";
      break;
    case "Rain":
      icons.main = "wi-rain";
      icons.img = "https://whenintrovertspeaks.files.wordpress.com/2013/10/rain-photography-wallpaper.jpg";
      break;
    case "Snow":
      icons.main = "wi-snow";
      icons.img = "https://az616578.vo.msecnd.net/files/2016/01/09/635879112155223228-319755513_635861833670816810507191518_6670-perfect-snow-1920x1080-nature-wallpaper.jpg";
      break;
    case "Extreme":
      icons.main = 'wi-hurricane';
      icons.img = "https://upload.wikimedia.org/wikipedia/commons/0/04/Hurricane_Isabel_from_ISS.jpg";
      break;
    default:
      icons.main = "wi-na";
      icons.img = "http://www.australia.com/content/australia/en/facts/weather/_jcr_content/mainParsys/hero/image.adapt.1663.medium.jpg";
      break;
  }
  if (json.main.temp > 25) {
    icons.temp = "wi-hot"
  } else if (json.main.temp < 0) {
    icons.temp = "wi-snowflake-cold";
  } else {
    icons.temp = "";
  }
  icons.wind = "wi-windy";
  return icons;
}
            
          
!
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.

Console