I am looking for a way to enhance the appearance of my <div>
contents when my website loads. They should gradually appear one after the other as the website loads. Additionally, the background image should load slowly due to being filtered by the weatherType
.
This is a webpage dedicated to local weather that provides real-time weather information based on the user's location.
Note:- To access the API, please share the code on third-party Web Development platforms like codepen.io
.
Here's the Code:
$(document).ready(function() {
$(".text-center").fadeIn();
var lon, lat, weatherType, ftemp, ktemp, ctemp, wspeed;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
lon = position.coords.longitude;
lat = position.coords.latitude;
var api = 'https://api.openweathermap.org/data/2.5/forecast?lat=' + lat + '&lon=' + lon + '&appid=bb4b778076b0c8c79c7eb8fcd1fd4330';
$.getJSON(api, function(data) {
// $("#data").html(api);
var city = data.city.name;
weatherType = data.list[0].weather[0].description;
//weatherType="clear sky";
ktemp = data.list[0].main.temp;
console.log(ktemp);
ftemp = (9 / 5 * (ktemp - 273) + 32).toFixed(1);
ctemp = (5 / 9 * (ftemp - 32)).toFixed(1);
wspeed = data.list[0].wind.speed;
wspeed = (wspeed * 5 / 18).toFixed(1);
/* $("#city").addClass("animated fadein",function(){
$("#city").html(city);
}); */
$("#city").addClass("animated fadein");
$("#city").html(city);
$("#weatherType").html(weatherType);
$("#temp").html(ctemp + " ℃");
//$("[name='my-checkbox']").bootstrapSwitch();
$("#degree-toggle").attr("value", $("<div/>").html("℉").text());
var celsius = true;
$("#degree-toggle").on("click", function() {
if (celsius === true) {
$("#temp").html(ftemp + " ℉");
$("#temp").fadeIn();
$("#degree-toggle").attr("value", $("<div/>").html("℃").text());
celsius = false;
} else {
$("#temp").html(ctemp + " ℃");
$("#temp").fadeIn();
$("#degree-toggle").attr("value", $("<div/>").html("℉").text());
celsius = true;
}
});
$("#wspeed").html(wspeed + " kmph");
weatherType=weatherType.toLowerCase();
if (weatherType === "clear sky")
$("body").css("background-image", "url('https://static.pexels.com/photos/281260/pexels-photo-281260.jpeg')");
else if (weatherType === "few clouds")
$("body").css("background-image", "url('https://clearalliance.org/wp-content/uploads/2015/01/CLEAR-see-clear-flowers-e1422658973500.jpg')");
else if (weatherType === "cloudy")
$("body").css("background-image", "url('http://www.gazetteseries.co.uk/resources/images/5360796/')");
else if (weatherType === "sunny")
$("body").css("background-image","url('https://i2-prod.examiner.co.uk/incoming/article10372520.ece/ALTERNATES/s1227b/JS75768352.jpg')");
else if (weatherType==="showers")
$("body").css("background-image","url('http://ak8.picdn.net/shutterstock/videos/1479838/thumb/1.jpg')");
else if(weatherType==="overcast clouds")
$("body").css("background-image","url('https://patchegal.files.wordpress.com/2012/07/img_2406.jpg')");
else if(weatherType==="light rain")
$("body").css("background-image","url('https://i.ytimg.com/vi/LbAigABOm_E/maxresdefault.jpg')");
else
$("body").css("background-image","url('https://www.almanac.com/sites/default/files/image_nodes/thanksgiving-weather.jpg')");
});
});
}
});
.text-center{
display: none;
}
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700&lang=en" /><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="text-center" id="content">
<div> <h1><b>Weather Today</b></h1></div><br/>
<h2>Location : <span id="city"></span></h2> <br/>
<h2>Weather : <span id="weatherType"></span></h2><br/>
<h2>Temperature : <span id="temp">
</span>
<input type="button" id="degree-toggle" checked="checked">
</h2><br/>
<h2>Wind Speed : <span id="wspeed"></span></h2><br/>
</div>
</body>
</html>