Build a jQuery Loading Overlay Spinner Using AJAX on Page Load in Browser Using HTML5 & Javascript Full Project For Beginners

 

 

 

index.html

 

 

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>jQuery Loading Overlay Demo</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

  <script>
      /*! Loading Overlay - v1.0.2 - 2014-02-19
* http://jgerigmeyer.github.io/jquery-loading-overlay/
* Copyright (c) 2014 Jonny Gerig Meyer; Licensed MIT */
(function ($) {

'use strict';

var methods = {
  init: function (options) {
    var opts = $.extend({}, $.fn.loadingOverlay.defaults, options);
    var target = $(this).addClass(opts.loadingClass);
    var overlay = '<div class="' + opts.overlayClass + '">' +
      '<p class="' + opts.spinnerClass + '">' +
      '<span class="' + opts.iconClass + '"></span>' +
      '<span class="' + opts.textClass + '">' + opts.loadingText + '</span>' +
      '</p></div>';
    // Don't add duplicate loading-overlay
    if (!target.data('loading-overlay')) {
      target.prepend($(overlay)).data('loading-overlay', true);
    }
    return target;
  },

  remove: function (options) {
    var opts = $.extend({}, $.fn.loadingOverlay.defaults, options);
    var target = $(this).data('loading-overlay', false);
    target.find('.' + opts.overlayClass).detach();
    if (target.hasClass(opts.loadingClass)) {
      target.removeClass(opts.loadingClass);
    } else {
      target.find('.' + opts.loadingClass).removeClass(opts.loadingClass);
    }
    return target;
  },

  // Expose internal methods to allow stubbing in tests
  exposeMethods: function () {
    return methods;
  }
};

$.fn.loadingOverlay = function (method) {
  if (methods[method]) {
    return methods[method].apply(
      this,
      Array.prototype.slice.call(arguments, 1)
    );
  } else if (typeof method === 'object' || !method) {
    return methods.init.apply(this, arguments);
  } else {
    $.error('Method ' + method + ' does not exist on jQuery.loadingOverlay');
  }
};

/* Setup plugin defaults */
$.fn.loadingOverlay.defaults = {
  loadingClass: 'loading',          // Class added to target while loading
  overlayClass: 'loading-overlay',  // Class added to overlay (style with CSS)
  spinnerClass: 'loading-spinner',  // Class added to loading overlay spinner
  iconClass: 'loading-icon',        // Class added to loading overlay spinner
  textClass: 'loading-text',        // Class added to loading overlay spinner
  loadingText: 'loading'            // Text within loading overlay
};

}(jQuery));
  </script>
  <style>
      /* Loading-overlay styles */

@font-face {
  font-family: "demo";
  src: url('fonts/icons.woff') format("woff"), url('fonts/icons.ttf') format("truetype");
}

@-moz-keyframes loadingStart {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
@-webkit-keyframes loadingStart {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
@-o-keyframes loadingStart {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
@keyframes loadingStart {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
@-moz-keyframes loading {
  0% {
    -moz-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  50% {
    -moz-transform: rotate(180deg);
    transform: rotate(180deg);
  }
  100% {
    -moz-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
@-webkit-keyframes loading {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  50% {
    -webkit-transform: rotate(180deg);
    transform: rotate(180deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
@-o-keyframes loading {
  0% {
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  50% {
    -o-transform: rotate(180deg);
    transform: rotate(180deg);
  }
  100% {
    -o-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
@keyframes loading {
  0% {
    transform: rotate(0deg);
  }
  50% {
    transform: rotate(180deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.loading {
  position: relative;
  pointer-events: none;
}

#css-input:checked ~ .loading .loading-overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  -webkit-animation: loadingStart 3s 300ms linear 1 both;
  -moz-animation: loadingStart 3s 300ms linear 1 both;
  -o-animation: loadingStart 3s 300ms linear 1 both;
  animation: loadingStart 3s 300ms linear 1 both;
  background: rgba(255, 255, 255, 0.5);
  text-align: center;
}
#css-input:checked ~ .loading .loading-text {
  font-size: 0.875rem;
  line-height: 1.3125rem;
  text-shadow: white 0 0 1em, white 0 0 0.5em, white 0 0 0.25em;
  position: relative;
  display: block;
  text-transform: uppercase;
  font-weight: bold;
}
#css-input:checked ~ .loading .loading-text:after {
  content: "...";
}
#css-input:checked ~ .loading .loading-spinner {
  position: absolute;
  top: 50%;
  bottom: 0;
  left: 0;
  right: 0;
  margin: -3.9375rem auto 0;
  color: #1a1d1d;
  text-align: center;
}
#css-input:checked ~ .loading .loading-icon {
  font-size: 4.8125rem;
  line-height: 5.25rem;
  text-shadow: rgba(255, 255, 255, 0.75) 0 0 0.5em;
  -webkit-animation: loading 1s steps(4) infinite;
  -moz-animation: loading 1s steps(4) infinite;
  -o-animation: loading 1s steps(4) infinite;
  animation: loading 1s steps(4) infinite;
  display: block;
  vertical-align: middle;
}
#css-input:checked ~ .loading .loading-icon:before {
  vertical-align: middle;
  content: "\e000";
  font-family: "demo";
}

/* Demo page styles */

* {
  box-sizing: border-box;
}

html {
  font-family: "Franklin Gothic Medium", "Franklin Gothic", "ITC Franklin Gothic", Arial, sans-serif;
}

.demo {
  min-width: 34em;
  max-width: 52em;
  margin: 1em auto;
  padding: 1em;
}

.demo-content {
  text-align: right;
}

#target {
  clear: both;
  width: 100%;
  height: 2em;
  margin-bottom: 1em;
  padding: 0 0 55% 1em;
  border: 2px solid black;
  text-align: left;
}

#css-input:checked ~ #target {
  background: url('http://placekitten.com/g/800/600') no-repeat;
  background-size: cover;
}

.toggle-loading {
  float: left;
  margin-bottom: 1em;
  padding: 1em 2em;
  border: none;
  border-radius: .25em;
  background: pink;
  font: inherit;
  font-size: 1.25em;
  text-transform: uppercase;
  color: white;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.5);
  transition: all 150ms;
}

.toggle-loading:focus {
  outline: none;
  box-shadow: 0 0 1px 2px rgba(0, 0, 0, 0.1) inset;
}

.toggle-loading:hover {
  cursor: pointer;
  background: #fd9297;
}

.toggle-loading:active {
  background: #bc6d71;
  box-shadow: 0 0 1px 2px rgba(0, 0, 0, 0.25) inset;
}

label[for="css-input"] {
  display: inline-block;
  padding-top: 3em;
}

pre {
  overflow: auto;
  font-family: "Courier New", Courier, "Lucida Sans Typewriter", "Lucida Typewriter", monospace, serif;
  font-size: .85em;
  line-height: 1.5em;
  color: grey;
}

.code-samples .js {
  clear: both;
  margin-bottom: 1em;
}

.code-samples .html {
  clear: both;
  border-top: 1px dashed;
  border-color: grey;
  margin-bottom: 1em;
}

.code-samples .css {
  clear: both;
  border-top: 1px dashed;
  border-color: grey;
}
  </style>
</head>
<body>
  <div class="demo">
    <div class="demo-content">
      <button class="toggle-loading">Toggle Overlay</button>
      <input type="checkbox" id="css-input" checked />
      <label for="css-input">show with demo css</label>
      <div id="target"></div>
    </div>
    <div class="code-samples">
      <div class="js">
        <h2>JavaScript: </h2>
        <pre>
jQuery(function ($) {
  var target = $('#target');

  $('.toggle-loading').click(function () {
    if (target.hasClass('loading')) {
      target.loadingOverlay('remove');
    } else {
      target.loadingOverlay();
    };
  });
});
        </pre>
      </div>
      <div class="html">
        <h2>HTML without overlay: </h2>
        <pre><div id="target"></div></pre>
        <h2>HTML with overlay: </h2>
        <pre>
<div id="target" class="loading">
  <div class="loading-overlay">
    <p class="loading-spinner">
      <span class="loading-icon"></span>
      <span class="loading-text">loading</span>
    </p>
  </div>
</div>
        </pre>
      </div>
      <div class="css">
        <h2>Demo CSS: </h2>
        <pre>
@font-face {
  font-family: "demo";
  src: url('fonts/icons.woff') format("woff"), url('fonts/icons.ttf') format("truetype");
}

@keyframes loadingStart {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
@keyframes loading {
  0% {
    transform: rotate(0deg);
  }
  50% {
    transform: rotate(180deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.loading {
  position: relative;
  pointer-events: none;
}

#css-input:checked ~ .loading .loading-overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  -webkit-animation: loadingStart 3s 300ms linear 1 both;
  -moz-animation: loadingStart 3s 300ms linear 1 both;
  -o-animation: loadingStart 3s 300ms linear 1 both;
  animation: loadingStart 3s 300ms linear 1 both;
  background: rgba(255, 255, 255, 0.5);
  text-align: center;
}
#css-input:checked ~ .loading .loading-text {
  font-size: 0.875rem;
  line-height: 1.3125rem;
  text-shadow: white 0 0 1em, white 0 0 0.5em, white 0 0 0.25em;
  position: relative;
  display: block;
  text-transform: uppercase;
  font-weight: bold;
}
#css-input:checked ~ .loading .loading-text:after {
  content: "...";
}
#css-input:checked ~ .loading .loading-spinner {
  position: absolute;
  top: 50%;
  bottom: 0;
  left: 0;
  right: 0;
  margin: -3.9375rem auto 0;
  color: #1a1d1d;
  text-align: center;
}
#css-input:checked ~ .loading .loading-icon {
  font-size: 4.8125rem;
  line-height: 5.25rem;
  text-shadow: rgba(255, 255, 255, 0.75) 0 0 0.5em;
  -webkit-animation: loading 1s steps(4) infinite;
  -moz-animation: loading 1s steps(4) infinite;
  -o-animation: loading 1s steps(4) infinite;
  animation: loading 1s steps(4) infinite;
  display: block;
  vertical-align: middle;
}
#css-input:checked ~ .loading .loading-icon:before {
  vertical-align: middle;
  content: "\e000";
  font-family: "demo";
}
        </pre>
      </div>
    </div>
  </div>
  <script>
  jQuery(function ($) {
    var target = $('#target');

    $('.toggle-loading').click(function () {
      if (target.hasClass('loading')) {
        target.loadingOverlay('remove');
      } else {
        target.loadingOverlay();
      };
    });
  });
  </script>
</body>
</html>

Leave a Reply