| 1 |
/** |
| 2 |
* ThePreloader.js |
| 3 |
* A lightweight preloader plugin. |
| 4 |
* |
| 5 |
* @author Alobaidi |
| 6 |
* @version 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
class ThePreloader { |
| 10 |
constructor(options = {}) { |
| 11 |
this.options = { |
| 12 |
preloaderElement: typeof options.preloaderElement === 'string' && options.preloaderElement.trim() |
| 13 |
? options.preloaderElement.toLowerCase().trim() |
| 14 |
: 'the-preloader-element' |
| 15 |
}; |
| 16 |
|
| 17 |
this.preloader = document.getElementById(this.options.preloaderElement); |
| 18 |
|
| 19 |
this.init = this.init.bind(this); |
| 20 |
this.hidePreloader = this.hidePreloader.bind(this); |
| 21 |
} |
| 22 |
|
| 23 |
fadeOut(element, duration) { |
| 24 |
return new Promise(resolve => { |
| 25 |
element.style.transition = `opacity ${duration}s ease-in-out`; |
| 26 |
element.style.opacity = '0'; |
| 27 |
|
| 28 |
setTimeout(() => { |
| 29 |
element.style.display = 'none'; |
| 30 |
resolve(); |
| 31 |
}, duration * 1000 + 50); |
| 32 |
}); |
| 33 |
} |
| 34 |
|
| 35 |
async hidePreloader() { |
| 36 |
await new Promise(resolve => |
| 37 |
setTimeout(resolve, 500) |
| 38 |
); |
| 39 |
|
| 40 |
await this.fadeOut(this.preloader, 0.5); |
| 41 |
|
| 42 |
setTimeout(() => { |
| 43 |
this.preloader.remove(); |
| 44 |
}, 50); |
| 45 |
} |
| 46 |
|
| 47 |
init() { |
| 48 |
if ( !this.preloader ) { |
| 49 |
console.log('ThePreloader.js: Preloader element does not exist.'); |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
this.hidePreloader(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
document.addEventListener('DOMContentLoaded', function() { |
| 58 |
|
| 59 |
window.addEventListener('load', () => { |
| 60 |
const the_Preloader = new ThePreloader({ |
| 61 |
preloaderElement: 'the-preloader-element' |
| 62 |
}); |
| 63 |
the_Preloader.init(); |
| 64 |
}); |
| 65 |
|
| 66 |
}); |