PageSourceSearch

https://westcozcafe.com/Content/customizable_loading/js/modal-loading.js?v=235.20260916-120

js westcozcafe.com collected 2026-09-30 10:41:04 UTC 11,342 bytes, 435 lines download raw bytes

1/**
2 * Modal Loading JavaScript Library
3 * @author 						c
4 * @date    					2017-11-06
5 * @param  {window} 	global  
6 * @param  {jQuery} 	$       
7 * @param  {function} 	factory 
8 * @return {void}         		
9 * @version 1.0.0
10 */
11(function(window, $, factory) {
12
13	window.Loading = factory(window, $);
14
15})(window, jQuery, function(window, $) {
16
17	var windowWidth;
18	var windowHeight;
19
20	/**
21	 * 构造Loading
22	 * @author  				c
23	 * @date 					2017-11-06
24	 * @param {Object} options	构造Loading的具体参数
25	 * @return {Loading} 		Loading对象
26	 */
27	function Loading(options) {
28		return new Loading.prototype._init($('body'), options);
29	}
30
31	/**
32	 * 初始化函数
33	 * @author  				c
34	 * @date 					2017-11-06
35	 * @param {Object} $this	jQuery对象
36	 * @param {Object} options	构造Loading的具体参数
37	 * @return {Loading} 		Loading对象
38	 */
39	const init = Loading.prototype._init = function($target, options) {
40		
41		this.version = '1.0.0';
42
43		this.$target = $target;
44
45		this.set = $.extend(true, {}, this.set, options);
46
47		this._build();
48
49		return this;
50
51	};
52
53	/**
54	 * 构建Loading
55	 * @return {void} 
56	 */
57	Loading.prototype._build = function() {
58
59		this.$modalMask = $('<div class="modal-mask"></div>');
60
61		this.$modalLoading = $('<div class="modal-loading custom-loading"></div>');
62
63		this.$loadingTitle = $('<p class="loading-title"></p>');
64
65		this.$loadingAnimation = $('<div class="loading-animate"></div>');
66
67		this.$animationOrigin = $('<div class="animate-origin"><span></span></div>');
68
69		this.$animationImage = $('<img/>');
70
71		this.$loadingDiscription = $('<p class="loading-discription"></p>');
72
73		// zIndex
74		if(this.set.zIndex <= 0) {
75			this.set.zIndex = (this.$target.siblings().length-1 || this.$target.children().siblings().length) + 10001;
76		}
77
78		// var attr, value;
79		// for(attr in this.set) {
80		// 	if(attr !== 'zIndex' && attr !== 'animationDuration') {
81		// 		value = this.set[attr];
82		// 		if(typeof value === 'number') {
83		// 			if(value <= 0) {
84		// 				this.set[attr] = 'auto';
85		// 			} else {
86		// 				this.set[attr] = (value + this.set.unit);
87		// 			}
88		// 		}
89		// 	}
90		// }
91
92		// 构建Loading
93		this._buildMask();
94
95		this._buildLoading();
96
97		this._buildTitle();
98		
99		this._buildLoadingAnimation();
100		
101		this._buildDiscription();
102
103		// 是否初始化过
104		this._init = false;
105
106		if(this.set.defaultApply) {
107			this.apply();
108		}
109
110	}
111
112	/**
113	 * 构建Mask
114	 * @return {void} 
115	 */
116	Loading.prototype._buildMask = function() {
117
118		// 如果不适用遮罩层
119		if(!this.set.mask) {
120			this.$modalMask.css({
121				position: 	'absolute',
122				top: 		'-200%',
123			});
124			return ;
125		}
126
127		// 遮罩层样式
128		this.$modalMask.css({
129			backgroundColor: 	this.set.maskBgColor,
130			zIndex: 			this.set.zIndex,
131		});
132
133		// 添加额外的class
134		this.$modalMask.addClass(this.set.maskClassName);
135
136	}
137
138	/**
139	 * 构建Loading
140	 * @return {void} 
141	 */
142	Loading.prototype._buildLoading = function() {
143
144		this.$modalLoading.css({
145			width: 				this.set.loadingWidth,
146			height: 			this.set.loadingHeight,
147			padding: 			this.set.loadingPadding,
148			backgroundColor: 	this.set.loadingBgColor,
149			borderRadius: 		this.set.loadingBorderRadius,
150		});
151
152		// 布局方式
153		if(this.set.direction === 'hor') {
154			this.$modalLoading.addClass('modal-hor-layout');
155		}
156
157		// 将loading添加到mask中
158		this.$modalMask.append(this.$modalLoading);
159
160	}
161
162	/**
163	 * 构建Title
164	 * @return {void} 
165	 */
166	Loading.prototype._buildTitle = function() {
167
168		if(!this.set.title) {
169			return ;
170		}
171
172
173		this.$loadingTitle.css({
174			color: 		this.set.titleColor,
175			fontSize: 	this.set.titleFontSize,
176		});
177
178		this.$loadingTitle.addClass(this.set.titleClassName);
179
180		this.$loadingTitle.text(this.set.title);
181
182		// 将title添加到loading中
183		this.$modalLoading.append(this.$loadingTitle);
184
185	}
186
187	/**
188	 * 构建LoadingAnimation
189	 * @return {void} 
190	 */
191	Loading.prototype._buildLoadingAnimation = function() {
192
193		// loadingAnimation
194		this.$loadingAnimation.css({
195			width: this.set.animationWidth,
196			height: this.set.animationHeight,
197		});
198
199		if(this.set.loadingAnimation === 'origin') { // origin动画
200			this.$animationOrigin.children().css({
201				width: this.set.animationOriginWidth,
202				height: this.set.animationOriginHeight,
203				backgroundColor: this.set.animationOriginColor,
204			});
205			for(var i = 0; i < 5; i++) {
206				this.$loadingAnimation.append(this.$animationOrigin.clone());
207			}
208		}
208 else if(this.set.loadingAnimation === 'image') { // 图片加载动画
209			this.$animationImage.attr('src', this.set.animationSrc);
210			this.$loadingAnimation.append(this.$animationImage);
211		} //else {
212		// 	throw new Error("[loadingAnimation] 参数错误. 参数值只能为['origin', 'image']");
213		// }
214
215		this.$loadingAnimation.addClass(this.set.animationClassName);
216
217		// 将loadingAnimation添加到loading中
218		this.$modalLoading.append(this.$loadingAnimation);
219
220	}
221
222	/**
223	 * 构建Discription
224	 * @return {void} 
225	 */
226	Loading.prototype._buildDiscription = function() {
227
228		if(!this.set.discription) {
229			return ;
230		}
231
232		this.$loadingDiscription.css({
233			color: 		this.set.discriptionColor,
234			fontSize: 	this.set.discriptionFontSize,
235		});
236
237		this.$loadingDiscription.addClass(this.set.discriptionClassName);
238
239		this.$loadingDiscription.text(this.set.discription);
240
241		// 将title添加到loading中
242		this.$modalLoading.append(this.$loadingDiscription);
243
244	}
245
246	/**
247	 * 定位
248	 * @return {void} 
249	 */
250	Loading.prototype._position = function() {
251
252		windowWidth = $(window).width();
253		windowHeight = $(window).height(); 
254
255		var loadingWidth = this.$modalLoading.outerWidth();
256		var loadingHeight = this.$modalLoading.outerHeight();
257
258		var x1 = windowWidth >>> 1;
259		var x2 = loadingWidth >>> 1;
260		var left = x1 - x2;
261
262		var y1 = windowHeight >>> 1;
263		var y2 = loadingHeight >>> 1;
264		var top = y1 - y2;
265
266		this.$modalLoading.css({ top, left });
267
268	}
269
270	/**
271	 * 入屏过度动画
272	 * @return {void} 
273	 */
274	Loading.prototype._transitionAnimationIn = function() {
275
276		if(!this.set.animationIn) {
277			this.$modalMask.css({ display: 'block' });
278		} else {
279			// this.$modalMask.removeClass(this.set.animationOut).addClass(this.set.animationIn);
280			this.$modalMask.addClass(this.set.animationIn);
281		}
282		
283	}
284
285	/**
286	 * 出屏过度动画
287	 * @return {void} 
288	 */
289	Loading.prototype._transitionAnimationOut = function() {
290
291		
292		if(!this.set.animationOut) {
293			
294			// this.$modalMask.css({ display: 'none' });
295			this.$modalMask.remove();
296
297		} else {
298			
299			this._timer && this._timer.clearTimeout(this._timer);
300
301			this.$modalMask.removeClass(this.set.animationIn).addClass(this.set.animationOut);
302
303			// this._timer = setTimeout(() => {
304			// 	this.$modalMask.remove();
305			// }, this.set.animationDuration);
306
307			var self = this;
308
309			this._timer = setTimeout(function() {
310				self.$modalMask.remove();
311			}, this.set.animationDuration);
312
313		}
314	}
315
316	/**
317	 * 显示Loading
318	 * @return {void} 
319	 */
320	Loading.prototype.apply = function() {
321		this._transitionAnimationIn();
322
323		// 这样按理说可以增加性能, 因为不需要从内存中寻找_initLoading方法.
324		if(!this._init) {
325			// 初始化Loading
326			this._initLoading();
327		}
328
329	}
330
331	/**
332	 * 隐藏Loading
333	 * @return {void} 
334	 */
335	Loading.prototype.out = function() {
336		this._transitionAnimationOut();
337	}
338
339	/**
340	 * 初始化Loading
341	 * @return {void} 
342	 */
343	Loading.prototype._initLoading = function() {
344
345		// 已经初始过 无需再次初始化
346		if(this._init) {
347			return ;
348		}
349
350		// 添加到页面中
351		this.$target.append(this.$modalMask);
352
353		// 定位
354		this._position();
355
356		// $(window).resize(() => {
357		// 	windowWidth = $(window).width();
358		// 	windowHeight = $(window).height();
359		// 	this._position();
360		// });
361
362		var self = this;
363		
364		$(window).resize(function() {
365			windowWidth = $(window).width();
366			windowHeight = $(window).height();
367			self._position();
368		});
369
370		this._init = true;
371	}
372
373	/**
374	 * Loading参数属性
375	 * 可以简单的设置一些css样式, 复杂的css样式可以通过增加class来更改样式.
376	 *
377	 * 像素单位: 如果是字符串, 则原文设置. 如果是数字类型, 默认单位为{unit}. zIndex除外.
378	 *
379	 * 如果字体样式为undefined(例如: titleFontFamily), 那么将会适用全局的字体样式(fontFamily)
380	 * 
381	 * @author  c
382	 * @date 	2017-11-06
383	 * @version 1.0.0
384	 */
385	Loading.prototype.set = {
386		direction: 				'ver',	 					// 方向. ver: 垂直, hor: 水平.
387
388		title: 					undefined, 					// 标题内容.
389		titleColor: 			'#FFF', 					// 标题文字颜色.
390		titleFontSize: 			14, 						// 标题文字字体大小. 
391		titleClassName: 		undefined,					// 标题额外的class值.
392		// titleFontFamily: 	undefined,					// 标题字体样式
393		
394		discription: 			undefined, 					// 描述内容.
395		discriptionColor: 		'#FFF',						// 描述文字颜色.
396		discriptionFontSize: 	14,							// 描述文字字体大小. 
397		discriptionClassName: 	undefined,					// 描述额外的class值.
398		// directionFontFamily: undefined,					// 描述字体样式.
399
400		loadingWidth: 			'auto',						// Loading宽度.
401		loadingHeight: 			'auto',						// Loading高度.
402		loadingPadding: 		20,							// Loading内边距.
403		loadingBgColor: 		'#252525',					// Loading背景颜色.
404		loadingBorderRadius: 	12,							// Loading的borderRadius.
405		// loadingPosition: 		'fixed',					// Loading的position
406
407		mask: 					true, 						// 遮罩层. true: 显示遮罩层, false: 不显示. 
408		maskBgColor: 			'rgba(0, 0, 0, .6)',		// 遮罩层背景颜色.
409		maskClassName: 			undefined,					// 为遮罩层添加.
410		// maskPosition: 			'fixed',					// 遮罩层position
411
412		loadingAnimation: 		'origin',					// 加载动画. origin: 表示使用默认的原点动画, image: 表示使用自定义图片作为加载动画.
413		animationSrc: 			undefined,					// 图片加载动画的地址. (前提: loadingAnimation=origin, 以下简称origin或者image)
414		animationWidth: 		40, 						// 动画宽度. 为image时表示图片的宽度.
415		animationHeight: 		40,							// 动画高度. 为image时表示图片的高度.
416		animationOriginWidth:   4,							// 原点动画宽度.    (前提: origin)
417		animationOriginHeight:  4,							// 原点动画高度.    (前提: origin)
418		animationOriginColor:   '#FFF',						// 原点动画的颜色.  (前提: origin)
419		animationClassName: 	undefined,					// 为动画添加一个额外的class值.
420		
421		defaultApply: 			true,						// 默认自动显示.
422		animationIn: 			'animated fadeIn', 			// 入屏动画. 
423		animationOut: 			'animated fadeOut',			// 出屏动画.
424		animationDuration: 		1000,						// 动画持续时间(单位:ms)
425		// fontFamily: 			'sans-serif',				// 文字字体样式.
426		// position: 				'fixed',				// 定位. mask和loading的定位.
427		// unit: 				'px', 						// 设置默认单位.
428		zIndex: 				0,							// 最外围层级(mask). 如果是0或者负数, 则为{$this.siblings() + 10001}.
429
430	};
431
432	init.prototype = Loading.prototype;
433
434	return Loading;
435});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.