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.

            
              <html>
  <head>
    <title>Weather App</title>
    <!--jQuery-->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    
    <!--jQuery UI-->
    <script type="text/javascript" src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
    
    <!--CSS-->
    <link rel="stylesheet" href="css/style.css" type="text/css">
    
    <!--JS-->
    <script type="text/javascript" src="js/main.js"></script>
    
    <!--Bootstrap-->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    
    <!--Font Awesome Icons-->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
      
    <!--Font Library - GlacialIndifferenceRegular-->
    <link rel="stylesheet" media="screen" href="https://fontlibrary.org/face/glacial-indifference" type="text/css"/>

  </head>
  
  <body>
    <div class="jumbotron text-center">
      
      <div class="title page-header">
        <h1>Weather App</h1>
      </div> <!--end page-header-->
      
        <div class="row temp">
          <div class="col-xs-4 col-xs-offset-4 col-md-6 col-md-offset-3">  
            <h3>
              <span class="temp-box-c"><h1></h1></span>
              <span class="weather-desc"></span>
            </h3>  
         </div>
      </div> <!--end row temp-->
      
      <div class="row humidity-wind-bar">
        <div class="humidity col-xs-5">
          <h4><strong></strong></h4>
        </div>
        <div class="wind-speed col-xs-7">
          <h4><strong></strong></h4>
        </div>
      </div> <!--end humidity-wind-bar-->
      
      <div class="more-info text-right">
        <div class="row-fluid">
          <div class="location panel">
            <h2 class="city"></h2>
            <h3 class="longitude"></h3>
            <h3 class="latitude"></h3>
          </div> <!--end location-->
          
          <div class="temp-min-max panel">
            <h3 class="temp-min"></h3>
            <h3 class="temp-max"></h3>
          </div> <!--end temp-min-max-->
            
          <div class="sun-rise-set panel">
            <h3 class="sunrise"></h3>
            <h3 class="sunset"></h3>
          </div> <!--end sun-rise-set-->
        </div> <!--end row-fluid-->
      </div> <!--end more-info-->
      
    </div>
  </body>
  
  <footer class="text-center"><h3>Built by <a href="https://codepen.io/neeraj-lad/" target="_blank" style="color: yellow;"><strong>Neeraj Lad</strong></h3></a></footer>
</html>
            
          
!
            
              body {
  background: url() center fixed;
  background-size: cover;
  #background-size: 100% auto;
  background-repeat: no-repeat;
}

.jumbotron {
  background-color: transparent !important; 
  margin: 0 auto;
  width: 45%;
  padding: 5%;
  
  font-family: 'GlacialIndifferenceRegular'; 
  font-weight: normal; 
  font-style: normal;
}

.temp {
  background: url();
  background-repeat: no-repeat;
  cursor: default;
}

.humidity-wind-bar {
  background-color: lightcyn;
  border-bottom: 2px dotted black;
}

.more-info {
  margin-top: 3%;
}

.location {
  background-color: white;
  opacity: 0.7;
  padding-right: 3%;
}

.location:hover {
  opacity: 1;
}

.location h2 {
  color: green;
}

.temp-min-max {
  background-color: white;
  opacity: 0.7;
  padding-right: 3%;
}

.temp-min-max:hover {
  opacity: 1;
}

.sun-rise-set {
  background-color: white;
  opacity: 0.7;
  padding-right: 3%;
}

.sun-rise-set:hover {
  opacity: 1;
}
            
          
!
            
              $(document).ready(function() {
  
  
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {
      
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
      /*
      //Cleveland, US
      var latitude = 	41.505493;
      var longitude = -81.681290;
      
      //London, UK
      var latitude = 	51.507351;
      var longitude = -0.127758;
     
      //Cairo, EG
      var latitude = 	30.044420;
      var longitude = 31.235712;
      
      //Kuala Lumpur, MY
      var latitude = 	3.139003;
      var longitude = 101.686855;
   
      //Vladivostok, RU
      var latitude = 	43.173739;
      var longitude = 132.006451;
      
      //Aukland, NZ
      var latitude = 	-36.848460;
      var longitude = 174.763332;
      
      //Barrio, Brazil
      var latitude = 	-33.448890;
      var longitude = -70.669265;
      */
      
      
      var openWeatherURL = "http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&appid=02261d500dcbc574f090866acaf2ec2a";
      
      console.log(openWeatherURL);
    
    $.getJSON(openWeatherURL, function(json) {
      
        var temperature = kelvinToCelsius(roundTo2Decimals(json["main"]["temp"]));
        var weatherDesc = json["weather"][0]["description"];
        weatherDesc = weatherDesc.substr(0, 1).toUpperCase() + weatherDesc.substr(1);
      
        //row1      
        var humidity = json["main"]["humidity"];
        var windSpeed = json["wind"]["speed"];
        
        //row2  
        var city = json["name"];
        var country = json["sys"]["country"];
        var location = city + ", " + country;
      
        //row3
        var longitude = json["coord"]["lon"];
        var latitude = json["coord"]["lat"];
        
        //row4
        var tempMin = kelvinToCelsius(roundTo2Decimals(json["main"]["temp_min"]));
        var tempMax = kelvinToCelsius(roundTo2Decimals(json["main"]["temp_max"]));
        
        //row5
        var sunrise = getFormattedTime(json["sys"]["sunrise"]);
        var sunset = getFormattedTime(json["sys"]["sunset"]);
        
      /*Weather icons:
      Types of weather
      1. thunderstorm
      2. snow
      3. rain
      4. mist
      5. clouds
      6. clear sky
      */
      
      var weatherType = 0;
      if (weatherDesc.indexOf("thunder") > -1)
        weatherType = 1;
      else if (weatherDesc.indexOf("snow") > -1)
        weatherType = 2;
      else if (weatherDesc.indexOf("rain") > -1)
        weatherType = 3;
      else if (weatherDesc.indexOf("mist") > -1)
        weatherType = 4;
      else if (weatherDesc.indexOf("cloud") > -1)
        weatherType = 5;
      else if (weatherDesc.indexOf("clear") > -1)
        weatherType = 6;
      
      var bckgImg = "", weatherIcon = "";
      switch (weatherType)
        {
          case 1: bckgImg = "https://pixabay.com/static/uploads/photo/2015/07/27/14/23/flash-862620_960_720.jpg";
                  weatherIcon = "http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/128/Status-weather-storm-night-icon.png";
                  break;
          case 2: bckgImg = "https://pixabay.com/static/uploads/photo/2015/11/05/22/36/blizzard-1025002_960_720.jpg";
                  weatherIcon = "http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/128/Status-weather-snow-icon.png";
                  break;
          case 3: bckgImg = "https://pixabay.com/static/uploads/photo/2016/01/15/09/28/still-1141345_960_720.jpg";
                  weatherIcon = "http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/128/Status-weather-showers-scattered-icon.png";
                  break;
          case 4: bckgImg = "https://pixabay.com/static/uploads/photo/2015/11/07/11/53/mountain-1031602_960_720.jpg";
                  weatherIcon = "http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/128/Status-weather-many-clouds-icon.png";
                  break;
          case 5: bckgImg = "https://pixabay.com/static/uploads/photo/2014/09/30/00/06/storm-466677_960_720.jpg";
                  weatherIcon = "http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/128/Status-weather-clouds-icon.png";
                  break;
          case 6: bckgImg = "https://pixabay.com/static/uploads/photo/2016/01/02/01/51/clouds-1117584_960_720.jpg";
                  weatherIcon = "http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/128/Status-weather-clear-icon.png";
                  break;
          default: bckgImg = "https://pixabay.com/static/uploads/photo/2016/01/02/01/51/clouds-1117584_960_720.jpg";
                   weatherIcon = "http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/128/Status-weather-clear-icon.png"; 
        }

      $("body").css("background-image", "url( "+ bckgImg + ")");
      $(".temp").css("background-image", "url( " + weatherIcon + ")");
        //update values  
    
        $(".temp-box-c").html("<h1>" + temperature + " &#xb0;C</h1>");
        $(".weather-desc").html("<h3>" + weatherDesc + "</h3>");
       
        //row1
        $(".humidity").html("<h4>Humidity: <strong>" + humidity + " %</strong></h4>");
        $(".wind-speed").html("<h4>Wind Speed: <strong>" + windSpeed + " m/s</strong></h4>");
        
        //row2
        $(".city").html("<h2>" + location + "</h2>");   
        $(".longitude").html("<h3>Longitude: " + longitude + "</h3>");
        $(".latitude").html("<h3>Latitude: " + latitude + "</h3>");
      
        //row3
        $(".temp-min").html("<h3>Temp(Min): " + tempMin + "&#xb0; C</h3>");
        $(".temp-max").html("<h3>Temp(Max): " + tempMax + "&#xb0; C</h3>");
        
        //row4
        $(".sunrise").html("<h3>Sunrise: " + sunrise + "</h3>");
        $(".sunset").html("<h3>Sunset: " + sunset + "</h3>");
   
    },
  
        function (error) { 
          if (error.code >=0 && error.code <= 3)
            $(".location").html("<h2><strong>Unable to fetch your current location. Please allow the browser to access <span style='color: red;'>Geolocation services</span>.</strong></h2>");
        }
  );

}); //end navigator.geolocation if clause
  } 
  else {
    $(".location").html("<h2><strong>Unable to fetch your current location. Please switch to a browser which supports <span style='color: red;'>Geolocation services</span>.</strong></h2>");
  }//end navigator.geolocation if-else clause
  
  //on clikc celsisus to fahrenheit feature
  $(".temp-box-c").on("click", function() {
      var currentClass = $(this).attr("class");
    
      if (currentClass === "temp-box-c")
      {
        var tempC = $(this).text();
        tempC = tempC.split(" ");
        tempC = tempC[0];
        var tempF = celsiusToFahrenheit(tempC);
        $(this).removeClass("temp-box-c");
        $(this).addClass("temp-box-f");
        $(this).html("<h1>" + tempF + " &#xb0;F</h1>");
      }
      else
      if (currentClass === "temp-box-f")
      {
        var tempF = $(this).text();
        tempF = tempF.split(" ");
        tempF = tempF[0];
        var tempC = fahrenheitToCelsius(tempF);
        $(this).removeClass("temp-box-f");
        $(this).addClass("temp-box-c");
        $(this).html("<h1>" + tempC + " &#xb0;C</h1>");
      }
      
  });
  
  
  //Helper functions
  function celsiusToFahrenheit(temp) {
      return roundTo2Decimals(temp * 9 / 5 + 32);
    } //end celsiusToFahrenheit  
  
    function fahrenheitToCelsius(temp) {
      return roundTo2Decimals((temp - 32) * 5 / 9); 
    }
  
    function getFormattedTime(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
        var formattedTime = hours + ':' + minutes.substr(-2);
            
        return formattedTime;
                    
    } //end getFormattedTime()
    
    function kelvinToCelsius(temp) {
        return Math.round(temp - 273);
    } //end kelvinToCelsius()
  
    function roundTo2Decimals(temp) {
      return Math.round (temp * 100) / 100;
    }
  
});
            
          
!
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