|
|
1
|
+/**
|
|
|
2
|
+ * Modal 覆盖层组件
|
|
|
3
|
+ *
|
|
|
4
|
+ * @author: aiden.xu<aiden.xu@yoho.cn>
|
|
|
5
|
+ * @date: 2016/07/18
|
|
|
6
|
+ */
|
|
|
7
|
+
|
|
|
8
|
+'use strict';
|
|
|
9
|
+
|
|
|
10
|
+const ANIMATIONS = {
|
|
|
11
|
+ fade: {
|
|
|
12
|
+ in: 'overlay-fade-in',
|
|
|
13
|
+ out: 'overlay-fade-out'
|
|
|
14
|
+ }
|
|
|
15
|
+};
|
|
|
16
|
+
|
|
|
17
|
+class Overlay {
|
|
|
18
|
+ constructor(opts) {
|
|
|
19
|
+ this.isVisible = false;
|
|
|
20
|
+
|
|
|
21
|
+ // 默认参数
|
|
|
22
|
+ this.defaults = {
|
|
|
23
|
+ className: 'overlay',
|
|
|
24
|
+ clickToClose: true, // 点击关闭
|
|
|
25
|
+ onClose: $.noop, // 关闭回调函数
|
|
|
26
|
+ animation: 'fade', // 动画效果
|
|
|
27
|
+ disableScrolling: true
|
|
|
28
|
+ };
|
|
|
29
|
+
|
|
|
30
|
+ // 初始化参数
|
|
|
31
|
+ this.settings = Object.assign({}, this.defaults, opts);
|
|
|
32
|
+
|
|
|
33
|
+ this.settings.animationClasses = {
|
|
|
34
|
+ in: ANIMATIONS[this.settings.animation].in,
|
|
|
35
|
+ out: ANIMATIONS[this.settings.animation].out
|
|
|
36
|
+ };
|
|
|
37
|
+
|
|
|
38
|
+ this.elem = $('<div/>', {
|
|
|
39
|
+ class: this.settings.className
|
|
|
40
|
+ });
|
|
|
41
|
+
|
|
|
42
|
+ this.elem.appendTo('body');
|
|
|
43
|
+
|
|
|
44
|
+ // 点击关闭
|
|
|
45
|
+ if (this.settings.clickToClose) {
|
|
|
46
|
+ this.elem.click(()=> {
|
|
|
47
|
+ this.hide();
|
|
|
48
|
+ });
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
|
51
|
+ if (this.settings.disableScrolling) {
|
|
|
52
|
+ // 覆盖层出现时阻止滚动
|
|
|
53
|
+ $(window).on('touchmove', (e)=> {
|
|
|
54
|
+ if (this.isVisible) {
|
|
|
55
|
+ e.preventDefault();
|
|
|
56
|
+ }
|
|
|
57
|
+ });
|
|
|
58
|
+ }
|
|
|
59
|
+ }
|
|
|
60
|
+
|
|
|
61
|
+ _clearStylesClasses() {
|
|
|
62
|
+ this.elem
|
|
|
63
|
+ .removeClass(this.settings.animationClasses.in)
|
|
|
64
|
+ .removeClass(this.settings.animationClasses.out);
|
|
|
65
|
+ }
|
|
|
66
|
+
|
|
|
67
|
+ /**
|
|
|
68
|
+ * 显示覆盖层
|
|
|
69
|
+ */
|
|
|
70
|
+ show() {
|
|
|
71
|
+ this._clearStylesClasses();
|
|
|
72
|
+ this.elem.css({
|
|
|
73
|
+ visibility: 'visible',
|
|
|
74
|
+ height: $(window).height() * window.devicePixelRatio
|
|
|
75
|
+ }).show().addClass(this.settings.animationClasses.in);
|
|
|
76
|
+ this.isVisible = true;
|
|
|
77
|
+
|
|
|
78
|
+ $('body').css({
|
|
|
79
|
+ overflow: 'hidden'
|
|
|
80
|
+ });
|
|
|
81
|
+ }
|
|
|
82
|
+
|
|
|
83
|
+ /**
|
|
|
84
|
+ * 关闭覆盖层
|
|
|
85
|
+ */
|
|
|
86
|
+ hide() {
|
|
|
87
|
+ this._clearStylesClasses();
|
|
|
88
|
+ const listener = () => {
|
|
|
89
|
+ // 一次性监听,动画完成事件触发后删除监听器
|
|
|
90
|
+ this.elem[0].removeEventListener('webkitTransitionEnd', listener);
|
|
|
91
|
+ this.elem.css({
|
|
|
92
|
+ visibility: 'hidden'
|
|
|
93
|
+ });
|
|
|
94
|
+ this._clearStylesClasses();
|
|
|
95
|
+ this.isVisible = false;
|
|
|
96
|
+ };
|
|
|
97
|
+
|
|
|
98
|
+ this.elem[0].addEventListener('webkitTransitionEnd', listener);
|
|
|
99
|
+ this.elem.addClass(this.settings.animationClasses.out);
|
|
|
100
|
+
|
|
|
101
|
+ if (this.settings.disableScrolling) {
|
|
|
102
|
+ $('body').css({
|
|
|
103
|
+ overflow: 'auto'
|
|
|
104
|
+ });
|
|
|
105
|
+ }
|
|
|
106
|
+ }
|
|
|
107
|
+}
|
|
|
108
|
+
|
|
|
109
|
+((function($) {
|
|
|
110
|
+ let inst = null;
|
|
|
111
|
+
|
|
|
112
|
+ $.overlay = opts => {
|
|
|
113
|
+ if (!inst) {
|
|
|
114
|
+ inst = new Overlay(opts);
|
|
|
115
|
+ }
|
|
|
116
|
+ return inst;
|
|
|
117
|
+ };
|
|
|
118
|
+})(jQuery));
|
|
|
119
|
+
|
|
|
120
|
+module.exports = Overlay; |