Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy"
        crossorigin="anonymous">
    <link rel="stylesheet" href="style.css"></link>
    <title>Document</title>
</head>

<body>
    <div class="container">
        <div class="mainBG">
            <h1 class="">Your Local Weather</h1>
            <div class>
                <h3 id="location"></h3>
                <div id="icon"></div>
            </div>
            <div>


            </div>
            <div class="row">
                <h3 class="col" id="temp"></h3>
                <h3 class="col" id="description"></h3>

            </div>
            <div class="row">
                <H3 class="col" id="temp_low"></H3>
                <H3 class="col" id="temp_high"></H3>
            </div>
            <div class="row">
                <h3 class="col" id="wind_speed"></h3>
                <h3 class="col" id="wind_direction"></h3>
            </div>
            <div class="row">
                <h3 class="col" id="sunrise"></h3>
                <h3 class="col" id="sunset"></h3>
            </div>
            <div id="convertBtn">

                <button class="btn " id="btnUnits" type="button"></button>
                <P class="">Convert Units</P>
            </div>
        </div>

    </div>
    <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
    <script type="text/javascript" src="main.js"></script>
    <script defer src="https://use.fontawesome.com/releases/v5.0.7/js/all.js"></script>
</body>

              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css?family=Krona+One|Ubuntu");

body {
  height: 100%;
  /* background-color: #82a43a; */
  background: linear-gradient(
    135deg,
    #ddb800 1%,
    #ebf1f6 25%,
    #abd3ee 77%,
    #89c3eb 97%,
    #d5ebfb 100%
  );
  /* background: url("https://images.unsplash.com/photo-1501769630498-260b0ac4458d?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=b0cc2e43268922227934ca72e380e69a&auto=format&fit=crop&w=2850&q=80"); */
  background-size: cover;
  background-repeat: no-repeat;
}
.row {
  /* margin-left: 20px !important; */
}

.container {
  display: flex;
  justify-content: center;
  width: 75%;
}

#btnUnits {
  color: white;
  background: rgb(17, 168, 206);
  padding: 0 20px 0px 20px;
  border: solid 1px rgba(10, 10, 10, 0.4);
  display: flex;
  align-self: right;
  justify-self: center;
  text-align: center;

  /* text-align: center; */
}
#convertBtn {
  opacity: 0;
  font-family: "Ubuntu", sans-serif;
  float: right;
  color: white;
  /* display: flex;
  align-items: center;
  justify-items: center; */
}

#temp {
  /* margin-right: 20px; */
}
#icon {
  text-align: center;
}
h1 {
  font-family: "Krona One", sans-serif;
  text-align: center;
  text-shadow: 1px 2px 10px rgb(245, 245, 245);
}
#location {
  text-align: center;
}

h1::after {
  content: "";
  background-color: yellow;
  height: 100%;
  width: 1vw;
}

h3,
p {
  font-family: "Ubuntu", sans-serif;
  color: rgba(17, 10, 1, 0.849);
  text-shadow: 1px 2px 5px rgb(245, 245, 245);
}

.mainBG {
/*   max-width: 900px; */
  background-color: rgba(209, 233, 236, 0.3);
  padding: 50px;
  margin-top: 50px;
  border: solid rgba(10, 10, 10, 0.4) 2px;
  border-radius: 10px;
}

              
            
!

JS

              
                var geoLoc = "";
var temp = "";
var temp_max = "";
var temp_min = "";
var cod = 0;
var description = "";
var weather_id = "";
var weather_icon = "";
var wind_speed = "";
var wind_direction = "";
var loc_name = "";
var temp_convert = 0;
var min_convert = 0;
var max_convert = 0;
var addIcon = "";
var country = "";
var sunrise = "";
var sunset = "";

$(document).ready(function() {
  
  // setTimeout(function() {
  //   $("#btnUnits").trigger("click");
  // }, 10);
  
  console.log("document ready");
  var geoLoc = "";
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      console.log(position.coords.longitude, position.coords.latitude);
      geoLoc = `${position.coords.latitude}&lon=${position.coords.longitude}`;
      console.log(geoLoc);

      $.ajax({
        method: "GET",
        url:
          "https://fcc-weather-api.glitch.me/api/current?lat=" +
          position.coords.latitude +
          "&lon=" +
          position.coords.longitude,

        dataType: "json"
      }).done(function(json) {
        loc_name = json.name;
        console.log(" loc_name = " + loc_name);
        temp = Math.floor(json.main.temp);
        console.log(" temp = " + json.main.temp);
        temp_max = json.main.temp_max;
        console.log(" temp_max = " + json.main.temp_max);
        temp_min = json.main.temp_min;
        console.log(" temp_min = " + json.main.temp_min);
        cod = json.cod;
        console.log(" cod = " + json.cod);
        description = json.weather[0].description;
        console.log(" description = " + json.weather[0].description);
        weather_id = json.weather[0].id;
        console.log(" weather id = " + json.weather[0].id);
        weather_icon = json.weather[0].icon;
        console.log(" weather icon = " + json.weather[0].icon);
        wind_speed = json.wind.speed;
        console.log(" wind speed = " + json.wind.speed);
        wind_direction = json.wind.deg;
        console.log(" wind dir = " + json.wind.deg);
        console.log(json);
        country = json.sys.country;
        temp_convert = Math.floor(temp);
        min_convert = Math.floor(temp_min);
        max_convert = Math.floor(temp_max);
        sunrise = convertTime(json.sys.sunrise);
        console.log("sunrise = " + sunrise);
        sunset = convertTime(json.sys.sunset);
        console.log("sunset = " + sunset);

        $("#temp").html(temp + units);
        $("#btnUnits").html(units);
        $("#description").html(titleCase(description));
        $("#location").html("Here in " + loc_name + ", " + country);
        $("#wind_speed").html("Wind Speed: " + wind_speed + " Knots");
        $("#temp_low").html("Today's Low " + temp_min + units);
        $("#temp_high").html("Today's High " + temp_max + units);
        $("#sunrise").html("Sunrise: " + sunrise);
        $("#sunset").html("Sunset: " + sunset);
        $("#convertBtn").css({ opacity: "1" });

        // weather_id = 200;
        degToCompass(wind_direction);
        addWeatherIcon();
        changeBgImage();
      });
    });
  }

  $.getJSON(geoLoc, function(json) {
    console.log(json);
  });

  var units = "℃";

  $("#btnUnits").on("click", function() {
    console.log(temp);
    console.log("temp_convert = " + temp_convert);
    if (units === "℃") {
      units = "℉";
      temp_convert = temp_convert * 1.8 + 32;
      min_convert = min_convert * 1.8 + 32;
      max_convert = max_convert * 1.8 + 32;
      console.log(temp_convert);
      $("#temp").html(Math.floor(temp_convert) + units + " ");
      $("#temp_low").html(
        "Today's Low " + Math.floor(min_convert) + units + " "
      );
      $("#temp_high").html(
        "Today's High " + Math.floor(max_convert) + units + " "
      );
    } else {
      units = "℃";
      temp_convert = (temp_convert - 32) / 1.8;
      min_convert = (min_convert - 32) / 1.8;
      max_convert = (max_convert - 32) / 1.8;
      $("#temp").html(Math.floor(temp_convert) + units + " ");
      $("#temp_low").html(
        "Today's Low " + Math.floor(min_convert) + units + " "
      );
      $("#temp_high").html(
        "Today's Hight " + Math.floor(max_convert) + units + " "
      );
    }
    $("#btnUnits").html(units);
    console.log("clicked!");
  });
});

function changeBgImage() {
  var x = weather_id;
  switch (true) {
    case x >= 200 && x <= 232:
      $("body").css({
        background:
          'url("https://images.unsplash.com/photo-1457528877294-b48235bdaa68?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=23776c88850b790975e42c214f781376&auto=format&fit=crop&w=2850&q=80")',

        "background-size": "cover",
        "background-repeat": "no-repeat"
      }),
        console.log("thunderstorm");
      break;
    case x >= 300 && x <= 321:
      $("body").css({
        background:
          'url("https://images.unsplash.com/photo-1506038044010-16649cc42188?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=7eb89ed46f743d4f841e8dd80d88ea1c&auto=format&fit=crop&w=2850&q=80)',

        "background-size": "cover",
        "background-repeat": "no-repeat"
      }),
        console.log("drizzle");
      break;
    case x >= 500 && x <= 531:
      $("body").css({
        background:
          'url("https://images.unsplash.com/photo-1438449805896-28a666819a20?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=950c1c30e781da5fa29e7db2f185c360&auto=format&fit=crop&w=2850&q=80)',

        "background-size": "cover",
        "background-repeat": "no-repeat"
      }),
        console.log("rain");
      break;
    case x >= 600 && x <= 622:
      $("body").css({
        background:
          'url("https://images.unsplash.com/photo-1484603738253-e5b73679e8cb?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=3d626d1bc5fda2b5ccc0c72a89d9c120&auto=format&fit=crop&w=2900&q=80)',

        "background-size": "cover",
        "background-repeat": "no-repeat"
      }),
        console.log("snow");
      break;
    case x === 800:
      $("body").css({
        background:
          'url("https://images.unsplash.com/photo-1445525994741-05c3738e5a89?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=83cf9757a1f0675d215ce8c14b124dea&auto=format&fit=crop&w=3150&q=80")',

        "background-size": "cover",
        "background-repeat": "no-repeat"
      }),
        console.log("clear sky");
      break;
    case x >= 801 && x <= 803:
      $("body").css({
        background:
          'url("https://images.unsplash.com/photo-1467740100611-36858db27485?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=55bd68817436c2ca6d8033acecd68531&auto=format&fit=crop&w=2853&q=80")',
        "background-size": "cover",
        "background-repeat": "no-repeat"
      }),
        console.log("thunderstorm"),
        console.log("partly cloudy");
      break;
    case x === 804:
      $("body").css({
        background:
          'url("https://images.unsplash.com/photo-1512187256255-409199cd3521?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=3c7465357d6624ded4f5cf21a87e68b6&auto=format&fit=crop&w=2850&q=80")',

        "background-size": "cover",
        "background-repeat": "no-repeat"
      }),
        console.log("cloudy");
      break;
    default:
      $("body").css({
        background:
          'url("https://images.unsplash.com/photo-1501769630498-260b0ac4458d?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=b0cc2e43268922227934ca72e380e69a&auto=format&fit=crop&w=2850&q=80")',

        "background-size": "cover",
        "background-repeat": "no-repeat"
      }),
        console.log("default image");
  }
}

function addWeatherIcon() {
  console.log("weather_Icon = " + weather_icon);
  addIcon += "<img src = '" + weather_icon + "'>";
  if (weather_icon) {
    $("#icon").html(addIcon);
  } else {
    $("#icon").html("<hr>");
  }
}

function degToCompass(deg) {
  var val = Math.floor(deg / 22.5 + 0.5);
  console.log(val);
  var arr = [
    "N",
    "NNE",
    "NE",
    "ENE",
    "E",
    "ESE",
    "SE",
    "SSE",
    "S",
    "SSW",
    "SW",
    "WSW",
    "W",
    "WNW",
    "NW",
    "NNW"
  ];
  console.log(val % 16);

  return $("#wind_direction").html("Wind Direction " + arr[val % 16]);
}

function titleCase(str) {
  return str
    .toLowerCase()
    .split(" ")
    .map(function(word) {
      return word.replace(word[0], word[0].toUpperCase());
    })
    .join(" ");
}

function convertTime(unix) {
  var date = new Date(unix * 1000);
  var hours = date.getHours();
  // hours = hours > 12 ? hours - 12 + " pm" : (hours = hours + " am");
  var time =
    hours > 12
      ? hours - 12 + ":" + date.getMinutes() + " pm"
      : hours + ":" + date.getMinutes() + " am";
  return time;
}

              
            
!
999px

Console