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.

            
              <head>
  
  <meta name="viewport" content="width=device-width, initial-scale=1">

</head>

<body>
  
    <div id='buttons'>
      <button id='button1' class='btn'>Right now</button> 
      <button id='button2' class='btn inactive'>5-day Forecast</button>
    </div>
    <div id='rightNow'>
      <div id='day'></div>
      <div id='icon'></div>
      <div id="weather">Weather: </div>
      <div id='temp'></div>
      <div id='humidity'></div>
      <div id="wind"></div>
    </div>
    
    
    <div id='forecast' class='row'>
      
      <div id='day1' class='col-xs-2'>
        <div id='day'>Today's date</div>
        <div id='icon'>&nbsp;</div>
        <div id='weather'>Sunny <i class='wi wi-day-sunny'></i></div>
        <div id='temp'>73.3°&nbsp;<i class='wi wi-thermometer'></i></div>
        <div id='humidity'>78&nbsp;<i class='wi wi-humidity'></i></div>
        <div id='wind'></div>
      </div>
      
      <div id='day2' class='col-xs-2'>
        <div id='day'></div>
        <div id='icon'></div>
        <div id='weather'></div>
        <div id='temp'></div>
        <div id='humidity'></div>
        <div id='wind'></div>
      </div>
      
      <div id='day3' class='col-xs-2'>
        <div id='day'></div>
        <div id='icon'></div>
        <div id='weather'></div>
        <div id='temp'></div>
        <div id='humidity'></div>
        <div id='wind'></div>
      </div>
      
      <div id='day4' class='col-xs-2'>
        <div id='day'></div>
        <div id='icon'></div>
        <div id='weather'></div>
        <div id='temp'></div>
        <div id='humidity'></div>
        <div id='wind'></div>
      </div>
      
      <div id='day5' class='col-lg-2'>
        <div id='day'></div>
        <div id='icon'></div>
        <div id='weather'></div>
        <div id='temp'></div>
        <div id='humidity'></div>
        <div id='wind'></div>
      </div>
    </div>
    
    <div id='footer' >Designed and coded by Aaron Key</div>
    
  </div>
</body>
            
          
!
            
              /* #### Mobile Phones Portrait #### */

@media screen and (max-device-width: 480px) and (orientation: portrait) {
  #rightNow {
    margin: 0;
    min-width: 100%;
    margin: 10px 0 0 0;
    height: auto;
    font-size: 6px;
  }
  
  #forecast div {
    min-width: 50%;
    border: none;
  }
}

html {
  min-heigh: 100%;
}

body {
  background-color: #08f;
}

#buttons {
  font: 20px bold serif;
  margin: 30px 0 20px 0;
  text-align: center;
}

.btn {
  color: white;
  background: #008;
  font: 22px bold Monospace;
  padding: 8px;
  border: 2px solid blue;
  border-radius: 12px;
  text-shadow: 1px 1px 12px #000;
}

.btn:hover {
  cursor: pointer;
  color: white;
}

#rightNow {
  border: 8px solid white;
  border-radius: 50px;
  text-align: center;
  width: 20%;
  padding: 10px 0 30px 0;
  margin: auto;
  text-shadow: 0px 0px 12px #fff;
}

#rightNow div {
  font: 22px bold Monospace;
  margin: 12px;
}

#forecast {
  display: none;
  text-align: center;
}

#forecast div {
  margin: auto;
  width: 20%;
  border: 2px solid white;
  border-radius: 5px;
}

#forecast div div {
  width: 100%;
  border: none;
  font: 22px bold Monospace;
  margin: 6px 0 0 0;
}

#footer {
  margin: 20px 0 10px 0;
  padding: 5px 0 0 0;
  border-top: 1px dotted black;
  text-align: center;
}

.wi-lg {
  font-size: 25px;
}

.inactive {
  font-size: 12px;
}
            
          
!
            
              var time, hour, min, lat, lon, data, datas, day, forecast, forecastJSON;
var api_key = '96dfc0518955e614bc07cae8e262041b';

//time conversion from UNIX timestamp
function timeConverter(UNIX_timestamp) {
  var a = new Date(UNIX_timestamp * 1000);
  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  var year = a.getFullYear();
  var month = months[a.getMonth()];
  var date = a.getDate();
  hour = a.getHours();
  min = a.getMinutes();
  var sec = a.getSeconds();
  time = month + ' ' + date;
}

//object for weather icons

var weatherIcons = {
  "01d": "<i class='wi-lg wi wi-day-sunny'></i>",
  "02d": "<i class='wi-lg wi wi-day-cloudy'></i>",
  "03d": "<i class='wi-lg wi wi-cloud'></i>",
  "04d": "<i class='wi-lg wi wi-cloudy'></i>",
  "09d": "<i class='wi-lg wi wi-day-showers'></i>",
  "10d": "<i class='wi-lg wi wi-day-rain'></i>",
  "11d": "<i class='wi-lg wi wi-storm-showers'></i>",
  "13d": "<i class='wi-lg wi wi-snow'></i>",
  "50d": "<i class='wi-lg wi wi-sprinkle'></i>",
  "01n": "<i class='wi-lg wi wi-stars'></i>",
  "02n": "<i class='wi-lg wi wi-night-alt-cloudy'></i>",
  "03n": "<i class='wi-lg wi wi-night-partly-cloudy'></i>",
  "04n": "<i class='wi-lg wi wi-cloudy'></i>",
  "09n": "<i class='wi-lg wi wi-night-alt-showers'></i>",
  "10n": "<i class='wi-lg wi wi-rain'></i>",
  "11n": "<i class='wi-lg wi wi-night-alt-lightning'></i>",
  "13n": "<i class='wi-lg wi wi-snowflake-cold'></i>",
  "50n": "<i class='wi-lg wi wi-night-alt-sprinkle'></i>",
}

//button functions to show current weather or 5 day forecast
$(document).ready(function() {
  $('#button1').click(function() {
    $('#rightNow').show();
    $('#forecast').hide();
    if ( $('#button1').hasClass("inactive") ) {
      $('#button1').removeClass("inactive");
      $('#button2').addClass("inactive");
    }
  });
  $('#button2').click(function() {
    $('#rightNow').hide();
    $('#forecast').show();
    if ( $('#button2').hasClass("inactive") ) {
      $('#button2').removeClass("inactive");
      $('#button1').addClass("inactive");
    }
  });
});

//function to determine cardinal direction from degrees
function cardinal(angle) {
  var directions = 8;

  var degree = 360 / directions;
  angle = angle + degree / 2;

  if (angle >= 0 * degree && angle < 1 * degree)
    return "N";
  if (angle >= 1 * degree && angle < 2 * degree)
    return "NE";
  if (angle >= 2 * degree && angle < 3 * degree)
    return "E";
  if (angle >= 3 * degree && angle < 4 * degree)
    return "SE";
  if (angle >= 4 * degree && angle < 5 * degree)
    return "S";
  if (angle >= 5 * degree && angle < 6 * degree)
    return "SW";
  if (angle >= 6 * degree && angle < 7 * degree)
    return "W";
  if (angle >= 7 * degree && angle < 8 * degree)
    return "NW";
  
  return "N";
}

$(document).ready(function() {
  if (navigator.geolocation) {
    //gets geolocation within 6 digits   
    navigator.geolocation.getCurrentPosition(function(position) {

      lat = position.coords.latitude.toFixed(8);
      lon = position.coords.longitude.toFixed(8);
      //data is api call for current weather
      data = 'http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&appid=' + api_key;
      //forecast is api call for forecast
      forecast = 'http://api.openweathermap.org/data/2.5/forecast?lat=' + lat + '&lon=' + lon + '&appid=' + api_key;

//-----------function to append current weather------------
      datas = $.getJSON(data, function() {
        
        timeConverter(datas.responseJSON.dt);
        $('#rightNow #day').append(time);
        $('#rightNow #icon').append(weatherIcons[datas.responseJSON.weather[0].icon]);
        $('#rightNow #weather').append(datas.responseJSON.weather[0].description);
        $('#rightNow #temp').append(Math.floor(datas.responseJSON.main.temp * 9 / 5 - 459.67) + "° F " + " " + "<i class='wi wi-thermometer'></i>");
        $('#rightNow #humidity').append("Humidity: " + datas.responseJSON.main.humidity + " " + "<i class='wi wi-humidity'></i>");
        $('#rightNow #wind').append("Wind: " + (datas.responseJSON.wind.speed * 2.24).toFixed(2) + " mph " + "<i class='wi-lg wi wi-wind towards-" + datas.responseJSON.wind.deg.toFixed(0) + "-deg'></i>" + " (" + cardinal(datas.responseJSON.wind.deg) + ")");
      });

//--------------funciton to append forecast-------------------

      forecastJSON = $.getJSON(forecast, function() {

        //day
        $('#forecast #day1 #day').empty();
        timeConverter(forecastJSON.responseJSON.list[3].dt);
        $('#forecast #day1 #day').append(time);
        timeConverter(forecastJSON.responseJSON.list[12].dt);
        $('#forecast #day2 #day').append(time);
        timeConverter(forecastJSON.responseJSON.list[19].dt);
        $('#forecast #day3 #day').append(time);
        timeConverter(forecastJSON.responseJSON.list[27].dt);
        $('#forecast #day4 #day').append(time);
        timeConverter(forecastJSON.responseJSON.list[35].dt);
        $('#forecast #day5 #day').append(time);
        
        
        //weather icon and time
        $('#forecast #day1 #icon').empty();
        $('#forecast #day1 #icon').append(weatherIcons[forecastJSON.responseJSON.list[3].weather[0].icon]);
        $('#forecast #day2 #icon').append(weatherIcons[forecastJSON.responseJSON.list[12].weather[0].icon]);
        $('#forecast #day3 #icon').append(weatherIcons[forecastJSON.responseJSON.list[19].weather[0].icon]);
        $('#forecast #day4 #icon').append(weatherIcons[forecastJSON.responseJSON.list[27].weather[0].icon]);
        $('#forecast #day5 #icon').append(weatherIcons[forecastJSON.responseJSON.list[35].weather[0].icon]);
        
        //weather description
        $('#forecast #day1 #weather').empty();
        $('#forecast #day1 #weather').append(forecastJSON.responseJSON.list[3].weather[0].description);
        $('#forecast #day2 #weather').append(forecastJSON.responseJSON.list[12].weather[0].description);
        $('#forecast #day3 #weather').append(forecastJSON.responseJSON.list[19].weather[0].description);
        $('#forecast #day4 #weather').append(forecastJSON.responseJSON.list[27].weather[0].description);
        $('#forecast #day5 #weather').append(forecastJSON.responseJSON.list[35].weather[0].description);
        //temperature
        $('#forecast #day1 #temp').empty();
        $('#forecast #day1 #temp').append((forecastJSON.responseJSON.list[3].main.temp * 9 / 5 - 459.67).toFixed(1) + "°" + " " + "<i class='wi wi-thermometer'></i>");
        $('#forecast #day2 #temp').append((forecastJSON.responseJSON.list[12].main.temp * 9 / 5 - 459.67).toFixed(1) + "°" + " " + "<i class='wi wi-thermometer'></i>");
        $('#forecast #day3 #temp').append((forecastJSON.responseJSON.list[19].main.temp * 9 / 5 - 459.67).toFixed(1) + "°" + " " + "<i class='wi wi-thermometer'></i>");
        $('#forecast #day4 #temp').append((forecastJSON.responseJSON.list[27].main.temp * 9 / 5 - 459.67).toFixed(1) + "°" + " " + "<i class='wi wi-thermometer'></i>");
        $('#forecast #day5 #temp').append((forecastJSON.responseJSON.list[35].main.temp * 9 / 5 - 459.67).toFixed(1) + "°" + " " + "<i class='wi wi-thermometer'></i>");

        //humidity
        $('#forecast #day1 #humidity').empty();
        $('#forecast #day1 #humidity').append("Humidity: " + forecastJSON.responseJSON.list[3].main.humidity + " " + "<i class='wi wi-humidity'></i>");
        $('#forecast #day2 #humidity').append("Humidity: " + forecastJSON.responseJSON.list[12].main.humidity + " " + "<i class='wi wi-humidity'></i>");
        $('#forecast #day3 #humidity').append("Humidity: " + forecastJSON.responseJSON.list[19].main.humidity + " " + "<i class='wi wi-humidity'></i>");
        $('#forecast #day4 #humidity').append("Humidity: " + forecastJSON.responseJSON.list[27].main.humidity + " " + "<i class='wi wi-humidity'></i>");
        $('#forecast #day5 #humidity').append("Humidity: " + forecastJSON.responseJSON.list[35].main.humidity + " " + "<i class='wi wi-humidity'></i>");

        //wind speed and direction
        $('#forecast #day1 #wind').append("Wind: " + (forecastJSON.responseJSON.list[3].wind.speed * 2.24).toFixed(2) + " mph " + "<i class='wi-lg wi wi-wind towards-" + forecastJSON.responseJSON.list[3].wind.deg.toFixed(0) + "-deg'></i>" + " (" + cardinal(forecastJSON.responseJSON.list[3].wind.deg) + ")");
        $('#forecast #day2 #wind').append("Wind: " + (forecastJSON.responseJSON.list[12].wind.speed * 2.24).toFixed(2) + " mph " + "<i class='wi-lg wi wi-wind towards-" + forecastJSON.responseJSON.list[12].wind.deg.toFixed(0) + "-deg'></i>" + " (" + cardinal(forecastJSON.responseJSON.list[12].wind.deg) + ")");
        $('#forecast #day3 #wind').append("Wind: " + (forecastJSON.responseJSON.list[19].wind.speed * 2.24).toFixed(2) + " mph " + "<i class='wi-lg wi wi-wind towards-" + forecastJSON.responseJSON.list[19].wind.deg.toFixed(0) + "-deg'></i>" + " (" + cardinal(forecastJSON.responseJSON.list[19].wind.deg) + ")");
        $('#forecast #day4 #wind').append("Wind: " + (forecastJSON.responseJSON.list[27].wind.speed * 2.24).toFixed(2) + " mph " + "<i class='wi-lg wi wi-wind towards-" + forecastJSON.responseJSON.list[27].wind.deg.toFixed(0) + "-deg'></i>" + " (" + cardinal(forecastJSON.responseJSON.list[27].wind.deg) + ")");
        $('#forecast #day5 #wind').append("Wind: " + (forecastJSON.responseJSON.list[35].wind.speed * 2.24).toFixed(2) + " mph " + "<i class='wi-lg wi wi-wind towards-" + forecastJSON.responseJSON.list[35].wind.deg.toFixed(0) + "-deg'></i>" + " (" + cardinal(forecastJSON.responseJSON.list[35].wind.deg) + ")");
      })

    })
  }
});
//example JSON response from current weather api call
//{"coord":{"lon":139,"lat":35},
//"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
//"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
//"main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
//"wind":{"speed":7.31,"deg":187.002},
//"rain":{"3h":0},
//"clouds":{"all":92},
//"dt":1369824698,
//"id":1851632,
//"name":"Shuzenji",
//"cod":200}
            
          
!
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