How can I pass the `<div>` elements of an animated recording when my site loads
I want my <div> content to record smoothly when my site loads. They should appear one after another when the site loads. In addition, the background image is loaded late when it is filtered by weatherType . This is a local Weather web page that gives you weather information based on where you open the page.
Note. - . Submit your code to other third-party sites, such as codepen.io , to access the API.
Here is my 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> At first, I would use animations to achieve a smooth recording, and not just for switch elements for visible ones.
Secondly, I would not write the elements manually, but what if you have 20 of them? What about 1000?
Start with such a frame, and then customize to your case:
HTML
<div id="elementsInThis"> <div class="K">ELEMENT 1</div> <div class="K">ELEMENT 2</div> <div class="K">ELEMENT 3</div> <div class="K">ELEMENT 4</div> <div class="K">ELEMENT 5</div> <div class="K">ELEMENT 6</div> <div class="K">ELEMENT 7</div> <div class="K">ELEMENT 8</div> <div class="K">ELEMENT 9</div> </div> Js
var delay = 0; function animate(element, delay){ window.setTimeout(function(){ element.style.display = 'block'; }, delay*1000) } var elements = document.getElementById("elementsInThis").childNodes; var onlydivs = Object.keys(elements).forEach(function(index, element){ if (elements[element].nodeType !== Node.TEXT_NODE) animate(elements[element], delay++); }); CSS
.K{ display: none; background: red; border: solid 2px black; animation-name: appear; animation-duration: 4s; animation-fill-mode: forwards; } @keyframes appear{from{left:-300px;opacity:0} to{left:0;opacity:1}} Here you can see it in action:
Change the animation and delay to your liking, as well as attach to your own parent and make the best children selector if you have more complex ones. This is just a demo, but the principle is the same that you do the following:
You want to use CSS to create a suitable (or find someone else's) animation and set the default display for the none element (perhaps instead of breaking your HTML code, you can use visibility)
make a delay function with a fixed delay step (I used a 1 * 1000 ms step)
use
childNodesforEach()andObject.keys()to avoid binding your CSS class with animation to elementsyou can leave the style in another CSS class if you like
So far I canβt give you an exact answer, as I am going to return home. What you can do is select a separate function for your animation function. Then, in the script, each of them will set the setTimeout event for each element, giving each of them a specific delay before showing. You can then add a transition or animation property to your element.
CSS
.elements { //css here } Js
//delay = time in ms before show, element = element to show. function letAnimate(delay, element){ window.setTimeout(function(){ element.style.display = 'block'; }, delay); } function startAnimate(){ letAnimate(1000, el1); //your first element which will show 1s after function call letAnimate(2000, el2); //2nd element, will show after 2s letAnimate(3000, el3); //3rd, 3s, so forth so on } window.onload = function(){ startAnimate(); }; //make this jquery on() or addEventListener. //You don't want this function to override window.onload event.