class Preloader { constructor(options) { this._options = Object.assign({}, this._defaultOptions, options); this._createElement(); this._count = 0; } get _defaultOptions() { return { text: 'Загрузка' }; } _createElement() { var wrapper = $('
', { class: 'kd-preloader wrapper' }); var preloader = $('
', { class: 'kd-preloader preloader' }); var text = $('', { class: 'kd-preloader text' }); var icon = $('', { class: 'kd-preloader icon icon-zagruzka' }); preloader.append(text, icon); this._element = wrapper.html(preloader); } show(text) { if (this._count === 0) { this._show(text); } this._count++; } _show(text) { this._element.find('.text').text(text || this._options.text); $('body').append(this._element); this._spinIcon(); } _spinIcon() { this._element.find('.icon').removeClass('active') setTimeout(() => {// Без таймера не стартует transition this._element.find('.icon').addClass('active'); }, 50); } hide() { if (this._count > 0) { this._count--; } if (this._count === 0) { this._hide(); } } hideAll() { this._count = 0; this._hide(); } _hide() { this._element.remove(); this._element.find('.preloader').removeClass('active'); } } $.preloader = new Preloader();