forked from Tencent/weui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
294 lines (269 loc) · 8.67 KB
/
example.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
$(function () {
var router = new Router({
container: '#container',
enterTimeout: 250,
leaveTimeout: 250
});
// grid
var home = {
url: '/',
className: 'home',
render: function () {
return $('#tpl_home').html();
}
};
// button
var button = {
url: '/button',
className: 'button',
render: function () {
return $('#tpl_button').html();
}
};
// cell
var cell = {
url: '/cell',
className: 'cell',
render: function () {
return $('#tpl_cell').html();
},
bind: function (){
$('.container').on('click', '#showTooltips', function (){
$('.js_tooltips').show();
setTimeout(function (){
$('.js_tooltips').hide();
}, 3000);
});
}
};
// toast
var toast = {
url: '/toast',
className: 'toast',
render: function () {
return $('#tpl_toast').html();
},
bind: function () {
$('#container').on('click', '#showToast', function () {
$('#toast').show();
setTimeout(function () {
$('#toast').hide();
}, 2000);
}).on('click', '#showLoadingToast', function () {
$('#loadingToast').show();
setTimeout(function () {
$('#loadingToast').hide();
}, 2000);
});
}
};
// dialog
var dialog = {
url: '/dialog',
className: 'dialog',
render: function () {
return $('#tpl_dialog').html();
},
bind: function () {
$('#container').on('click', '#showDialog1', function () {
$('#dialog1').show().on('click', '.weui_btn_dialog', function () {
$('#dialog1').off('click').hide();
});
}).on('click', '#showDialog2', function () {
$('#dialog2').show().on('click', '.weui_btn_dialog', function () {
$('#dialog2').off('click').hide();
});
});
}
};
// progress
var progress = {
url: '/progress',
className: 'progress',
render: function () {
return $('#tpl_progress').html();
},
bind: function () {
$('#container').on('click', '#btnStartProgress', function () {
if ($(this).hasClass('weui_btn_disabled')) {
return;
}
$(this).addClass('weui_btn_disabled');
var progress = 0;
var $progress = $('.js_progress');
function next() {
$progress.css({width: progress + '%'});
progress = ++progress % 100;
setTimeout(next, 30);
}
next();
});
}
};
// msg
var msg = {
url: '/msg',
className: 'msg',
render: function () {
return $('#tpl_msg').html();
}
};
// article
var article = {
url: '/article',
className: 'article',
render: function () {
return $('#tpl_article').html();
}
};
// actionsheet
var actionsheet = {
url: '/actionsheet',
className: 'actionsheet',
render: function () {
return $('#tpl_actionsheet').html();
},
bind: function () {
$('#container').on('click', '#showActionSheet', function () {
var mask = $('#mask');
var weuiActionsheet = $('#weui_actionsheet');
weuiActionsheet.addClass('weui_actionsheet_toggle');
mask.show()
.focus()//加focus是为了触发一次页面的重排(reflow or layout thrashing),使mask的transition动画得以正常触发
.addClass('weui_fade_toggle').one('click', function () {
hideActionSheet(weuiActionsheet, mask);
});
$('#actionsheet_cancel').one('click', function () {
hideActionSheet(weuiActionsheet, mask);
});
mask.unbind('transitionend').unbind('webkitTransitionEnd');
function hideActionSheet(weuiActionsheet, mask) {
weuiActionsheet.removeClass('weui_actionsheet_toggle');
mask.removeClass('weui_fade_toggle');
mask.on('transitionend', function () {
mask.hide();
}).on('webkitTransitionEnd', function () {
mask.hide();
})
}
});
}
};
// icons
var icons = {
url: '/icons',
className: 'icons',
render: function () {
return $('#tpl_icons').html();
}
};
// panel
var panel = {
url: '/panel',
className: 'panel',
render: function () {
return $('#tpl_panel').html();
}
};
// tab
var tab = {
url: '/tab',
className: 'tab',
render: function () {
return $('#tpl_tab').html();
}
};
// navbar
var navbar = {
url: '/navbar',
className: 'navbar',
render: function () {
return $('#tpl_navbar').html();
},
bind: function () {
$('#container').on('click', '.weui_navbar_item', function () {
$(this).addClass('weui_bar_item_on').siblings('.weui_bar_item_on').removeClass('weui_bar_item_on');
});
}
};
// tabbar
var tabbar = {
url: '/tabbar',
className: 'tabbar',
render: function () {
return $('#tpl_tabbar').html();
},
bind: function () {
$('#container').on('click', '.weui_tabbar_item', function () {
$(this).addClass('weui_bar_item_on').siblings('.weui_bar_item_on').removeClass('weui_bar_item_on');
});
}
};
// searchbar
var searchbar = {
url: '/searchbar',
className: 'searchbar',
render: function () {
return $('#tpl_searchbar').html();
},
bind: function () {
$('#container').on('focus', '#search_input', function () {
var $weuiSearchBar = $('#search_bar');
$weuiSearchBar.addClass('weui_search_focusing');
}).on('blur', '#search_input', function () {
var $weuiSearchBar = $('#search_bar');
$weuiSearchBar.removeClass('weui_search_focusing');
if ($(this).val()) {
$('#search_text').hide();
} else {
$('#search_text').show();
}
}).on('input', '#search_input', function () {
var $searchShow = $("#search_show");
if ($(this).val()) {
$searchShow.show();
} else {
$searchShow.hide();
}
}).on('touchend', '#search_cancel', function () {
$("#search_show").hide();
$('#search_input').val('');
}).on('touchend', '#search_clear', function () {
$("#search_show").hide();
$('#search_input').val('');
});
}
};
router.push(home)
.push(button)
.push(cell)
.push(toast)
.push(dialog)
.push(progress)
.push(msg)
.push(article)
.push(actionsheet)
.push(icons)
.push(panel)
.push(tab)
.push(navbar)
.push(tabbar)
.push(searchbar)
.setDefault('/')
.init();
// .container 设置了 overflow 属性, 导致 Android 手机下输入框获取焦点时, 输入法挡住输入框的 bug
// 相关 issue: https://github.com/weui/weui/issues/15
// 解决方法:
// 0. .container 去掉 overflow 属性, 但此 demo 下会引发别的问题
// 1. 参考 http://stackoverflow.com/questions/23757345/android-does-not-correctly-scroll-on-input-focus-if-not-body-element
// Android 手机下, input 或 textarea 元素聚焦时, 主动滚一把
if (/Android/gi.test(navigator.userAgent)) {
window.addEventListener('resize', function () {
if (document.activeElement.tagName == 'INPUT' || document.activeElement.tagName == 'TEXTAREA') {
window.setTimeout(function () {
document.activeElement.scrollIntoViewIfNeeded();
}, 0);
}
})
}
});