chemmens

HTML JavaScript Metro style loading bar improved

Jun 7th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>HTML Metro Loading Bar</title>
  5.         <style type="text/css">
  6.         body { background: #000 ;}
  7.         .block {
  8.             background: #0fF;
  9.             height: 5px;
  10.             width: 5px;
  11.         }
  12.         </style>
  13.     <head>
  14.     <body>
  15.         <script type="text/javascript">
  16.         LoadingBar = function (args /* { element : document element, numBlocks : integer } */) {
  17.  
  18.             var args = args == undefined ? { numBlocks: this.numBlocks, element: document.createElement('div')} : args;
  19.  
  20.             this.numBlocks = args.numBlocks == undefined ? this.numBlocks : typeof args.numBlocks == 'number' ? args.numBlocks : this.numBlocks;
  21.             this.element = args.element == undefined ? document.createElement('div') : args.element;
  22.  
  23.             this.element.className = 'loading-bar';
  24.             this.element.style.position = 'absolute';
  25.             this.element.style.left = '0';
  26.             this.element.style.right = '0';
  27.             this.element.style.overflow = 'hidden';
  28.             this.setDimensions();
  29.             var _this = this;
  30.             window.addEventListener('resize', function () { _this.setDimensions() });
  31.             return this;
  32.         }
  33.  
  34.         LoadingBar.prototype = {
  35.  
  36.             constructor: LoadingBar,
  37.             interval: null,
  38.             loops: 0,
  39.             loopPauseDuration: 1000,
  40.             element: null,
  41.             numBlocks: 5,
  42.             animationSpeed: 10,
  43.             blockDisplacemenetSpeed: 6,
  44.             slowDownPercentage: 50,
  45.             slowDownRange: 0,
  46.             slowDownAreaStart: 0,
  47.             slowDownAreaStop: 0,
  48.  
  49.             setDimensions: function () {
  50.                 this.element.style.width = window.innerWidth + 'px';
  51.                 this.slowDownRange = Math.floor((parseInt(this.element.style.width) / 3));
  52.                 this.slowDownAreaStart = this.slowDownRange;
  53.                 this.slowDownAreaStop = this.slowDownRange * 2;
  54.             },
  55.  
  56.             addBlock: function () {
  57.                 var block = document.createElement('div');
  58.                 block.className = 'block';
  59.                 block.style.position = 'fixed';
  60.                 block.style.display = 'block';
  61.                 this.element.appendChild(block);
  62.                 return block;
  63.             },
  64.  
  65.             removeBlock: function (index) {
  66.                 this.element.removeChild(this.element.getElementsByClassName('block')[index]);
  67.             },
  68.  
  69.             clearBlocks: function () {
  70.                 while (this.element.hasChildNodes()) {
  71.                     this.element.removeChild(this.element.lastChild);
  72.                 }
  73.             },
  74.  
  75.             start: function () {
  76.                 this.reset();
  77.                 for (var i = 0; i < this.numBlocks; i++) {
  78.                     this.addBlock().style.left = Math.floor(-1 * (1 + i) * (this.slowDownRange / this.numBlocks)) + 'px';
  79.                 }
  80.                 var pause = this.loops == 0 ? 0 : this.loopPauseDuration,
  81.                     _this = this;
  82.                 this.loops++;
  83.                 this.interval = setTimeout(function () { _this.animate(); }, pause);
  84.                 return this;
  85.             },
  86.  
  87.             animate: function () {
  88.                 var _this = this,
  89.                     blocks = this.element.getElementsByClassName('block');
  90.  
  91.                 if (blocks.length == 0) return this.start();
  92.  
  93.                 for (var i in blocks) {
  94.                     if (typeof blocks[i] == 'object') {
  95.                         var x = parseInt(blocks[i].style.left),
  96.                             displacementSpeed = this.blockDisplacemenetSpeed;
  97.  
  98.                         if (x >= this.slowDownAreaStart && x <= this.slowDownAreaStop) {
  99.                             displacementSpeed = Math.floor(displacementSpeed / (100 / this.slowDownPercentage));
  100.                         }
  101.                         blocks[i].style.left = (x + displacementSpeed) + 'px';
  102.  
  103.                         if (parseInt(blocks[i].style.left) >= parseInt(this.element.style.width)) {
  104.                             this.removeBlock(i)
  105.                         }
  106.                     }
  107.                 }
  108.                 this.interval = setTimeout(function () { _this.animate(); }, _this.animationSpeed);
  109.             },
  110.  
  111.             reset: function()
  112.             {
  113.                 this.clearBlocks();
  114.                 clearTimeout(this.interval);
  115.             },
  116.  
  117.             stop: function () {
  118.                 this.loops = 0;
  119.                 this.clearBlocks();
  120.                 clearTimeout(this.interval);
  121.             }
  122.         }
  123.        
  124.         document.body.appendChild(new LoadingBar().start().element);
  125.         </script>
  126.     </body>
  127. </html>
Advertisement
Add Comment
Please, Sign In to add comment