{"id":271,"date":"2019-03-17T10:35:40","date_gmt":"2019-03-17T16:35:40","guid":{"rendered":"https:\/\/justinstolpe.com\/blog\/?p=271"},"modified":"2019-03-17T10:50:25","modified_gmt":"2019-03-17T16:50:25","slug":"loading-overlay-with-html-css-and-jquery","status":"publish","type":"post","link":"https:\/\/justinstolpe.com\/blog\/2019\/03\/17\/loading-overlay-with-html-css-and-jquery\/","title":{"rendered":"Loading Overlay with HTML, CSS, and jQuery"},"content":{"rendered":"<p><center><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/nUjiqZLJWZQ\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/center>&nbsp;<\/p>\n<p>In this post we create a global loading overlay using HTML, CSS, and jQuery. There is one line of code to take over the screen and display a loader as well as one line of code to hide the loader. This loader is good for any button such as signing up, logging in, downloading files, and any other action that would require a loader to be displayed.<\/p>\n<h3><strong>Step 1: HTML and CSS<br \/>\n<\/strong><\/h3>\n<p>The HTML\/CSS for the loader is simple. It consists of two divs and a loading gif image. When the page loads, both divs are set to display none. Only when we have an action which requires the loader, do we actually want to show the loader.<\/p>\n<p>The first div, with a class &#8220;loading-overlay&#8221; is going to be our overlay. This will covers the entire screen. It gets a background of black with some opacity. This way we can see through to our website behind the overlay but we can&#8217;t interact with it at all because our overlay has take over. Position fixed and height\/width at 100% will make sure the overlay covers the entire browser window.<\/p>\n<p>The second div, with a class of &#8220;loading-overlay-image-container&#8221;, contains our loading gif image. This will sit on top of our &#8220;loading-overlay&#8221; div and be centered in the browser. Position fixed with top and left at 50% will get our image container centered in the browser.<\/p>\n<p><strong>HTML<\/strong><\/p>\n<pre class=\"lang:default decode:true\" title=\"loading overlay\">&lt;div class=\"loading-overlay\"&gt;&lt;\/div&gt;\r\n&lt;div class=\"loading-overlay-image-container\"&gt;\r\n    &lt;img src=\"assets\/loading.gif\" class=\"loading-overlay-img\"\/&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>CSS<\/p>\n<pre class=\"lang:css decode:true\" title=\"loading overlay css\">.loading-overlay {\r\n\tdisplay: none;\r\n\tbackground: rgba( 26, 26, 26, 0.7 );\r\n\tposition: fixed;\r\n\twidth: 100%;\r\n\theight: 100%;\r\n\tz-index: 5;\r\n\ttop: 0;\r\n}\r\n\r\n.loading-overlay-image-container {\r\n\tdisplay: none;\r\n\tposition: fixed;\r\n\tz-index: 7;\r\n\ttop: 50%;\r\n\tleft: 50%;\r\n\ttransform: translate( -50%, -50% );\r\n}\r\n\r\n.loading-overlay-img {\r\n\twidth: 50px;\r\n\theight: 50px;\r\n\tborder-radius: 5px;\r\n}<\/pre>\n<h3><strong>Step 2: JavaScript\/jQuery<br \/>\n<\/strong><\/h3>\n<p>Now that we have our HTML and CSS, we need to create a few JavaScript function to actually show and hide our loader. We are going to take it one step further and also have the HTML written out to the page with JavaScript\/jQuery on page load! To do this we are going to create an object with three functions.<\/p>\n<p><strong>initialize function<\/strong><\/p>\n<p>This function will write out our HTML to the body of our page. Call this function when the document is ready and the HTML gets appended to the body of our page. The loader is now ready to be used!<\/p>\n<p><strong>showLoader function<\/strong><\/p>\n<p>This function displays our loader on the page by changing the display none on both our divs, to display block.<\/p>\n<p><strong>hideLoader function<\/strong><\/p>\n<p>This function hides our loader on the page by changing the display block on both of our divs, to display none.<\/p>\n<p>Here is what the finished loader object looks like.<\/p>\n<pre class=\"lang:js decode:true \" title=\"loader.js\">\/**\r\n * Handle loading overlays\r\n *\r\n * @author Justin Stolpe\r\n *\/\r\nvar loader = {\r\n\t\/**\r\n\t * Initialize our loading overlays for use\r\n\t *\r\n\t * @params void\r\n\t *\r\n\t * @return void\r\n\t *\/\r\n\tinitialize : function () {\r\n\t\tvar html = \r\n\t\t\t'&lt;div class=\"loading-overlay\"&gt;&lt;\/div&gt;' +\r\n\t\t\t'&lt;div class=\"loading-overlay-image-container\"&gt;' +\r\n\t\t\t\t'&lt;img src=\"assets\/loading.gif\" class=\"loading-overlay-img\"\/&gt;' +\r\n\t\t\t'&lt;\/div&gt;';\r\n\r\n\t\t\/\/ append our html to the DOM body\r\n\t\t$( 'body' ).append( html );\r\n\t},\r\n\r\n\t\/**\r\n\t * Show the loading overlay\r\n\t *\r\n\t * @params void\r\n\t *\r\n\t * @return void\r\n\t *\/\r\n\tshowLoader : function () {\r\n\t\tjQuery( '.loading-overlay' ).show();\r\n\t\tjQuery( '.loading-overlay-image-container' ).show();\r\n\t},\r\n\r\n\t\/**\r\n\t * Hide the loading overlay\r\n\t *\r\n\t * @params void\r\n\t *\r\n\t * @return void\r\n\t *\/\r\n\thideLoader : function () {\r\n\t\tjQuery( '.loading-overlay' ).hide();\r\n\t\tjQuery( '.loading-overlay-image-container' ).hide();\r\n\t}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>Step 3: Putting it all Together<br \/>\n<\/strong><\/h3>\n<p>We have everything we need now for displaying a loader on our website. It is also easy to manage because everything is in our loader object. If we every need to update the loader, we simply update the loader object and the loader is update across our site!<\/p>\n<p>Here is an example of calling our loader. On page load we initialize the loader so it is ready for use. When the &#8220;Test&#8221; link gets clicked, we call show loader which will show our loader. After a few seconds, we call our hide loader function which hides the loader returning it to the state is was in on page load.<\/p>\n<pre class=\"lang:default decode:true\" title=\"loader example\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n\t&lt;head&gt;\r\n\t\t&lt;!-- include loader styles --&gt;\r\n\t\t&lt;link href=\"css\/global.css\" rel=\"stylesheet\" type=\"text\/css\"&gt;\r\n\r\n\t\t&lt;!-- include our loader overlay script --&gt;\r\n\t\t&lt;script type=\"text\/javascript\" src=\"js\/loader.js\"&gt;&lt;\/script&gt;\r\n\t\t\r\n\t\t&lt;script&gt;\r\n\t\t\t$( function() { \/\/ do things when the document is ready\r\n\t\t\t\t\/\/ initialize our loader overlay\r\n\t\t\t\tloader.initialize();\r\n\r\n\t\t\t\t$( '#load_test' ).on( 'click', function() { \/\/ on click for our load test link\r\n\t\t\t\t\t\/\/ show our loading overlay\r\n\t\t\t\t\tloader.showLoader();\r\n\r\n\t\t\t\t\tsetInterval( function() { \/\/ after 3 seconds, hide our loading overlay\r\n\t\t\t\t\t\tloader.hideLoader();\r\n\t\t\t\t\t}, 3000 );\r\n\t\t\t\t} );\r\n\t\t\t} );\r\n\t\t&lt;\/script&gt;\r\n\t&lt;\/head&gt;\r\n\t&lt;body&gt;\r\n\t\t&lt;span id=\"load_test\"&gt;Loading Overlay Test (lasts 3 sec)&lt;\/span&gt;\r\n\t&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><strong>Links<\/strong><\/p>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=nUjiqZLJWZQ\" target=\"_blank\" rel=\"noopener\"><strong>YouTube Video<\/strong><\/a><\/p>\n<p><strong><a href=\"https:\/\/github.com\/jstolpe\/easycodeis\" target=\"_blank\" rel=\"noopener\">Code on GitHub<\/a><\/strong><\/p>\n<p><a href=\"http:\/\/easycodeis.com\" target=\"_blank\" rel=\"noopener\"><strong>Live Example<\/strong><\/a><\/p>\n<p>That is going to do it for this post! Leave any comments\/questions\/concerns below and thanks for stopping by the blog!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; In this post we create a global loading overlay using HTML, CSS, and jQuery. There is one line of code to take over the screen and display a loader as well as one line of code to hide the loader. This loader is good for any button such as signing up, logging in, downloading &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/justinstolpe.com\/blog\/2019\/03\/17\/loading-overlay-with-html-css-and-jquery\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Loading Overlay with HTML, CSS, and jQuery&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":274,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,10,12,1],"tags":[15,13,14],"class_list":["post-271","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css","category-html","category-javascript","category-uncategorized","tag-css","tag-html","tag-javascript"],"_links":{"self":[{"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/posts\/271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/comments?post=271"}],"version-history":[{"count":5,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"predecessor-version":[{"id":276,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/posts\/271\/revisions\/276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/media\/274"}],"wp:attachment":[{"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}