-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
244 lines (222 loc) · 6.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
'use strict'
var fullscreen = {}
var opt = {
start: 0,
loop: false,
direction: 'v',
duration: 500,
distance: 0.1,
movingFlag: false,
preventWechat: false,
beforeChange: function (data) { },
afterChange: function (data) { }
}
fullscreen.install = function (Vue, options) {
var that = fullscreen
Vue.directive('fullscreen', {
inserted: function (el, binding, vnode) {
var opts = binding.value || {}
that.init(el, opts, vnode)
},
componentUpdated: function (el, binding, vnode) {
var opts = binding.value || {}
that.init(el, opts, vnode)
}
})
Vue.directive('animate', {
inserted: function (el, binding, vnode) {
if (binding.value) {
that.initAnimate(el, binding, vnode)
}
}
})
}
fullscreen.initAnimate = function (el, binding, vnode) {
var that = fullscreen;
var vm = vnode.context;
var aminate = binding.value;
el.style.opacity = '0';
vm.$on('toogle_animate', function (curIndex) {
var parent = el.parentNode;
while (parent.getAttribute('data-id') === null) {
parent = parent.parentNode;
}
var curPage = +parent.getAttribute('data-id');
if (Number(curIndex) === curPage) { //修复后端生成跳转事件,由于类型不匹配,页面跳转后动画无效的bug
that.addAnimated(el, aminate);
} else {
el.style.opacity = '0';
that.removeAnimated(el, aminate);
}
});
};
fullscreen.addAnimated = function (el, animate) {
var delay = animate.delay || 0
// console.log(el)
el.classList.add('animated')
window.setTimeout(function () {
el.style.opacity = '1'
el.classList.add(animate.value)
}, delay)
}
fullscreen.removeAnimated = function (el, animate) {
if (el.getAttribute('class').indexOf('animated') > -1) {
el.classList.remove(animate.value)
}
}
fullscreen.assignOpts = function (option) {
var that = fullscreen
var o = option || {}
for (var key in opt) {
if (!o.hasOwnProperty(key)) {
o[key] = opt[key]
}
}
that.o = o
}
fullscreen.initScrollDirection = function () {
if (this.o.direction !== 'v') {
this.el.classList.add('fullscreen-wp-h')
}
}
fullscreen.init = function (el, options, vnode) {
var that = fullscreen
that.assignOpts(options)
that.vm = vnode.context
that.vm.$fullscreen = that
if (typeof (that.curIndex) == "undefined") {
that.curIndex = that.o.start;
}
that.startY = 0
that.deltaX = 0
that.deltaY = 0
that.o.movingFlag = false
that.el = el
that.el.classList.add('fullscreen-wp')
that.parentEle = that.el.parentNode
that.parentEle.classList.add('fullscreen-container')
that.pageEles = that.el.children
that.total = that.pageEles.length
that.initScrollDirection()
window.setTimeout(function () {
that.width = that.parentEle.offsetWidth
that.height = that.parentEle.offsetHeight
for (var i = 0; i < that.pageEles.length; i++) {
var pageEle = that.pageEles[i]
pageEle.setAttribute('data-id', i)
pageEle.classList.add('page')
pageEle.style.width = that.width + 'px'
pageEle.style.height = that.height + 'px'
that.initEvent(pageEle)
}
// that.el.classList.add('anim');
that.moveTo(that.curIndex, false)
}, 0)
}
fullscreen.initEvent = function (el) {
var that = fullscreen
that.prevIndex = that.curIndex
el.addEventListener('touchstart', function (e) {
if (that.o.movingFlag) {
return false
}
that.startX = e.targetTouches[0].pageX
that.startY = e.targetTouches[0].pageY
})
el.addEventListener('mousewheel', function (e) {
if (that.o.movingFlag) {
return false
}
that.deltaX = e.deltaX
that.deltaY = e.deltaY
var direction = that.o.direction
if (that.deltaY > 0) {
var nextIndex = that.curIndex + 1
if (nextIndex < that.total) {
that.moveTo(nextIndex, true)
}
} else {
var nextIndex = that.curIndex - 1
if (nextIndex >= 0) {
that.moveTo(nextIndex, true)
}
}
})
el.addEventListener('touchend', function (e) {
if (that.o.movingFlag) {
return false
}
var direction = that.o.direction
var sub = direction === 'v' ? (e.changedTouches[0].pageY - that.startY) / that.height : (e.changedTouches[0].pageX - that.startX) / that.width
var distance = sub > that.o.distance ? -1 : sub < -that.o.distance ? 1 : 0
// that.curIndex推迟到moveTo执行完之后再更新
var nextIndex = that.curIndex + distance
if (nextIndex >= 0 && nextIndex < that.total) {
that.moveTo(nextIndex, true)
} else {
if (that.o.loop) {
nextIndex = nextIndex < 0 ? that.total - 1 : 0
that.moveTo(nextIndex, true)
} else {
that.curIndex = nextIndex < 0 ? 0 : that.total - 1
}
}
})
if (that.o.preventWechat) {
el.addEventListener('touchmove', function (e) {
e.preventDefault()
})
}
}
fullscreen.moveTo = function (curIndex, anim) {
var that = fullscreen;
var dist = that.o.direction === 'v' ? curIndex * - that.height : curIndex * - that.width;
that.o.movingFlag = true;
if (anim) {
var flag = that.o.beforeChange(that.prevIndex, curIndex);
if (flag === false) {
that.o.movingFlag = false;// 重置movingFlag
return false;
}
that.curIndex = curIndex;
if (anim) {
that.el.classList.add('anim');
} else {
that.el.classList.remove('anim');
}
that.move(dist);
var preValue = that.prevIndex;
window.setTimeout(function () {
that.o.afterChange(preValue, curIndex);
that.o.movingFlag = false;
that.prevIndex = curIndex;
that.vm.$emit('toogle_animate', curIndex);
}, that.o.duration);
} else {
window.setTimeout(function () {
that.o.movingFlag = false;
that.vm.$emit('toogle_animate', curIndex);
}, that.o.duration);
}
};
fullscreen.move = function (dist) {
var xPx = '0px'
var yPx = '0px'
if (this.o.direction === 'v') {
yPx = dist + 'px'
} else {
xPx = dist + 'px'
}
this.el.style.cssText += ('-webkit-transform:translate3d(' + xPx + ', ' + yPx + ', 0px);transform:translate3d(' + xPx + ', ' + yPx + ', 0px);')
}
if (typeof exports === 'object') {
module.exports = fullscreen
} else if (typeof define === 'function' && define.amd) {
define([], function () {
return fullscreen
})
} else if (window.Vue) {
window.Vuefullscreen = fullscreen
Vue.use(fullscreen)
}
export default fullscreen;