forked from dcloudio/mui
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmui.view.js
172 lines (149 loc) · 4.56 KB
/
mui.view.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
/**
* <div class=“mui-views">
<div class=“mui-view">
<div class=“mui-navbar">
<div class=“mui-navbar-inner”>
<div class=“mui-navbar-left”></div>
<div class=“mui-navbar-center”></div>
<div class=“mui-navbar-right”></div>
</div>
<div class=“mui-navbar-inner”>
<div class=“mui-navbar-left”></div>
<div class=“mui-navbar-center”></div>
<div class=“mui-navbar-right”></div>
</div>
</div>
<div class=“mui-pages”>
<div class=“mui-page”>
<div class=“mui-page-content"></div>
</div>
<div class=“mui-page”>
<div class=“mui-page-content"></div>
</div>
</div>
</div>
</div>
* @param {Object} $
* @param {Object} window
*/
(function($, window) {
var CLASS_PAGE_LEFT = $.className('page-left');
var CLASS_PAGE_CENTER = $.className('page-center');
var CLASS_TRANSITIONING = $.className('transitioning');
var SELECTOR_NAVBAR = $.classSelector('.navbar');
var SELECTOR_PAGES = $.classSelector('.pages');
var SELECTOR_PAGE_LEFT = '.' + CLASS_PAGE_LEFT;
var SELECTOR_PAGE_CENTER = '.' + CLASS_PAGE_CENTER;
var View = $.Class.extend({
init: function(element, options) {
this.view = this.element;
this.options = $.extend({
swipeBackPageActiveArea: 30,
hardwareAccelerated: true
}, options);
this.navbars = this.querySelector(SELECTOR_NAVBAR);
this.pages = this.querySelector(SELECTOR_PAGES);
this.maxScrollX = this.view.offsetWidth;
this.x = this.y = 0;
this.translateZ = this.options.hardwareAccelerated ? ' translateZ(0)' : '';
this.moved = this.dragging = false;
this.activePage = this.previousPage = null;
this.initEvent();
},
initEvent: function() {
this.view.addEventListener('drag', this);
this.view.addEventListener('dragend', this);
},
handleEvent: function(event) {
switch (e.type) {
case 'drag':
this._drag(event);
break;
case 'dragend':
this._dragend(event);
break;
}
},
_drag: function(event) {
var detail = event.detail;
if (!this.dragging) {
if ((detail.start.x - this.view.offsetLeft) < this.options.swipeBackPageActiveArea) {
this.previousPage = this.pages.querySelector(SELECTOR_PAGE_LEFT);
this.activePage = this.pages.querySelector(SELECTOR_PAGE_CENTER);
this.previousPageStyle = this.previousPage.style;
this.activePageStyle = this.activePage.style;
this.previousPage.classList.remove(CLASS_TRANSITIONING);
this.activePage.classList.remove(CLASS_TRANSITIONING);
if (this.previousPage && this.activePage) {
this.dragging = true;
$.gestures.session.lockDirection = true; //锁定方向
$.gestures.session.startDirection = detail.direction;
}
}
}
if (this.dragging) {
var deltaX = 0;
if (!this.moved) { //start
deltaX = detail.deltaX;
} else { //move
deltaX = detail.deltaX - detail.lastDeltaX;
}
var newX = this.x + deltaX;
if (newX > 0 || newX < this.maxScrollX) {
newX = newX > 0 ? 0 : this.maxScrollX;
}
event.stopPropagation();
detail.gesture.preventDefault();
if (!this.requestAnimationFrame) {
this._updateTranslate();
}
this.moved = true;
this.x = newX;
this.y = 0;
}
},
_dragend: function(event) {
if (!this.dragging) {
this.dragging = this.moved = false;
event.stopPropagation();
var detail = event.detail;
this._clearRequestAnimationFrame();
this.previousPage.classList.add(CLASS_TRANSITIONING);
this.activePage.classList.add(CLASS_TRANSITIONING);
return;
}
},
_clearRequestAnimationFrame: function() {
if (this.requestAnimationFrame) {
cancelAnimationFrame(this.requestAnimationFrame);
this.requestAnimationFrame = null;
}
},
_getTranslateStr: function(x, y) {
if (this.options.hardwareAccelerated) {
return 'translate3d(' + x + 'px,' + y + 'px,0px) ' + this.translateZ;
}
return 'translate(' + x + 'px,' + y + 'px) ';
},
_updateTranslate: function() {
var self = this;
if (self.x !== self.lastX || self.y !== self.lastY) {
self.setTranslate(self.x, self.y);
}
self.requestAnimationFrame = requestAnimationFrame(function() {
self._updateTranslate();
});
},
setTranslate: function(x, y) {
this.x = x;
this.y = y;
this.previousPageStyle['webkitTransform'] = this._getTranslateStr(x, y);
this.activePageStyle['webkitTransform'] = this._getTranslateStr(x, y);
if (this.navbars) {
this.navbars.setTranslate(x, y);
}
this.lastX = this.x;
this.lastY = this.y;
},
});
})(mui, window);