JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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="spinner load">
</div>
<div class="container text-center">
<h1>FreeCodeCamp</h1>
<h2>Local Weather App</h2>
<div id="city"></div>
<div class="temp">
<span id="temp"></span><span id="metric"></span>
</div>
<div id="weatherType"></div>
<span id="icon"></span>
<div class="search">
<label for="city-search">Find a forecast</label>
<input type="text" id="city-search" placeholder="ex. Los Angeles, CA" autofocus><br>
</div>
</div>
</div>
<footer class="text-center">photos by Liane Chan</footer>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyACfwNoW8oFr3ImAMkMaB04jKhE7jjQjo4&libraries=places,geometry">
</script>
body {
color: white;
align: center;
background: #888;
bacground-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
.container {
margin-top: 40px;
opacity: 0.75;
background-color: #333;
width: 400px;
padding: 40px;
}
#city {
font-size: 36px;
}
#temp, #weatherType {
font-size: 24px;
}
#weather-icon {
font-size: 36px;
}
footer {
margin-top: 250px;
}
$("body").css("background-image","url('https://lianechan.com/portfolio/wp-content/uploads/2015/05/DesertDaze2014.jpg')");
// locating you
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
let lat = position.coords.latitude;
let lon = position.coords.longitude;
getWeather(lat, lon);
});
} else {
alert("Sorry, Geolocation is not supported by this browser.");
}
// getting the weather data using fetch()
function getWeather(lat, lon) {
let api =
`https://fcc-weather-api.glitch.me/api/current?lat=${lat}&lon=${lon}`;
fetch(api)
.then(blob => blob.json())
.then(data => {
let cTemp = data.main.temp;
let city = data.name;
let country = data.sys.country;
let weatherType = data.weather[0].main;
let weatherDesc = data.weather[0].description;
let icon = data.weather[0].icon;
querySelector("#city").innerHTML = city + ", " + country;
querySelector("#weatherType").innerHTML =
weatherType + " (" + weatherDesc + ")";
querySelector("#icon").innerHTML =
"<img alt='o' src=" + icon + ">";
let fTemp = (cTemp * 9 / 5 + 32).toFixed(1);
let toggleCF = false;
let temp = querySelector("#temp");
let metric = querySelector("#metric");
temp.innerHTML = cTemp;
metric.innerHTML = " ℃";
metric.addEventListener("click", function() {
if (toggleCF === false) {
temp.innerHTML = fTemp;
metric.innerHTML = " ℉";
toggleCF = true;
} else {
temp.innerHTML = cTemp;
metric.innerHTML = " ℃";
toggleCF = false;
}
});
// console.log(weatherType);
switch(weatherType) {
case "Clear":
querySelector("body").style["background-image"] = "url(https://lianechan.com/portfolio/wp-content/uploads/2018/05/DSC0560.jpg)";
break;
case "Clouds":
querySelector("body").style["background-image"] = "url(https://lianechan.com/portfolio/wp-content/uploads/2018/05/P1000195.jpg)";
break;
case "Rain":
querySelector("body").style["background-image"] = "url(https://lianechan.com/portfolio/wp-content/uploads/2018/05/IMG_0855.jpg)";
break;
case "Mist":
querySelector("body").style["background-image"] = "url(https://lianechan.com/portfolio/wp-content/uploads/2013/12/IMG_1789.jpg)";
break;
case "Fog":
querySelector("body").style["background-image"] = "url(https://lianechan.com/portfolio/wp-content/uploads/2013/12/P1030177.jpg)";
break;
case "Thunderstorm":
querySelector("body").style["background-image"] = "url(https://lianechan.com/portfolio/wp-content/uploads/2018/05/DSC2204.jpg)";
break;
case "Snow":
querySelector("body").style['background-image'] = 'url(https://lianechan.com/portfolio/wp-content/uploads/2018/05/IMG_5565.jpg)';
break;
default:
querySelector("body").style["background-image"] = "url(https://lianechan.com/portfolio/wp-content/uploads/2015/05/DesertDaze2014.jpg)";
break;
}
querySelector('.spinner').classList.remove('load');
})
.catch(err => alert(err));
}
// making easy to get elements from the DOM and interecting with HTML
function querySelector(element) {
return document.querySelector(element);
}
// getting the weather for the searched city, -google api
let searchBox = new google.maps.places.Autocomplete(
document.querySelector("#city-search")
);
searchBox.addListener("place_changed", function() {
let locale = searchBox.getPlace();
let lat = locale.geometry.location.lat();
let lon = locale.geometry.location.lng();
querySelector('.spinner').classList.add('load');
getWeather(lat, lon);
});
Also see: Tab Triggers