Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>HTML Metro Loading Bar</title>
- <style type="text/css">
- body { background: #000 ;}
- .block {
- background: #0fF;
- height: 5px;
- width: 5px;
- }
- </style>
- <head>
- <body>
- <script type="text/javascript">
- LoadingBar = function (args /* { element : document element, numBlocks : integer } */) {
- var args = args == undefined ? { numBlocks: this.numBlocks, element: document.createElement('div')} : args;
- this.numBlocks = args.numBlocks == undefined ? this.numBlocks : typeof args.numBlocks == 'number' ? args.numBlocks : this.numBlocks;
- this.element = args.element == undefined ? document.createElement('div') : args.element;
- this.element.className = 'loading-bar';
- this.element.style.position = 'absolute';
- this.element.style.left = '0';
- this.element.style.right = '0';
- this.element.style.overflow = 'hidden';
- this.setDimensions();
- var _this = this;
- window.addEventListener('resize', function () { _this.setDimensions() });
- return this;
- }
- LoadingBar.prototype = {
- constructor: LoadingBar,
- interval: null,
- loops: 0,
- loopPauseDuration: 1000,
- element: null,
- numBlocks: 5,
- animationSpeed: 10,
- blockDisplacemenetSpeed: 6,
- slowDownPercentage: 50,
- slowDownRange: 0,
- slowDownAreaStart: 0,
- slowDownAreaStop: 0,
- setDimensions: function () {
- this.element.style.width = window.innerWidth + 'px';
- this.slowDownRange = Math.floor((parseInt(this.element.style.width) / 3));
- this.slowDownAreaStart = this.slowDownRange;
- this.slowDownAreaStop = this.slowDownRange * 2;
- },
- addBlock: function () {
- var block = document.createElement('div');
- block.className = 'block';
- block.style.position = 'fixed';
- block.style.display = 'block';
- this.element.appendChild(block);
- return block;
- },
- removeBlock: function (index) {
- this.element.removeChild(this.element.getElementsByClassName('block')[index]);
- },
- clearBlocks: function () {
- while (this.element.hasChildNodes()) {
- this.element.removeChild(this.element.lastChild);
- }
- },
- start: function () {
- this.reset();
- for (var i = 0; i < this.numBlocks; i++) {
- this.addBlock().style.left = Math.floor(-1 * (1 + i) * (this.slowDownRange / this.numBlocks)) + 'px';
- }
- var pause = this.loops == 0 ? 0 : this.loopPauseDuration,
- _this = this;
- this.loops++;
- this.interval = setTimeout(function () { _this.animate(); }, pause);
- return this;
- },
- animate: function () {
- var _this = this,
- blocks = this.element.getElementsByClassName('block');
- if (blocks.length == 0) return this.start();
- for (var i in blocks) {
- if (typeof blocks[i] == 'object') {
- var x = parseInt(blocks[i].style.left),
- displacementSpeed = this.blockDisplacemenetSpeed;
- if (x >= this.slowDownAreaStart && x <= this.slowDownAreaStop) {
- displacementSpeed = Math.floor(displacementSpeed / (100 / this.slowDownPercentage));
- }
- blocks[i].style.left = (x + displacementSpeed) + 'px';
- if (parseInt(blocks[i].style.left) >= parseInt(this.element.style.width)) {
- this.removeBlock(i)
- }
- }
- }
- this.interval = setTimeout(function () { _this.animate(); }, _this.animationSpeed);
- },
- reset: function()
- {
- this.clearBlocks();
- clearTimeout(this.interval);
- },
- stop: function () {
- this.loops = 0;
- this.clearBlocks();
- clearTimeout(this.interval);
- }
- }
- document.body.appendChild(new LoadingBar().start().element);
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment