example.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. $(function () {
  2. var router = new Router({
  3. container: '#container',
  4. enterTimeout: 250,
  5. leaveTimeout: 250
  6. });
  7. // grid
  8. var home = {
  9. url: '/',
  10. className: 'home',
  11. render: function () {
  12. return $('#tpl_home').html();
  13. }
  14. };
  15. // button
  16. var button = {
  17. url: '/button',
  18. className: 'button',
  19. render: function () {
  20. return $('#tpl_button').html();
  21. }
  22. };
  23. // cell
  24. var cell = {
  25. url: '/cell',
  26. className: 'cell',
  27. render: function () {
  28. return $('#tpl_cell').html();
  29. },
  30. bind: function (){
  31. $('.container').on('click', '#showTooltips', function (){
  32. $('.js_tooltips').show();
  33. setTimeout(function (){
  34. $('.js_tooltips').hide();
  35. }, 3000);
  36. });
  37. }
  38. };
  39. // toast
  40. var toast = {
  41. url: '/toast',
  42. className: 'toast',
  43. render: function () {
  44. return $('#tpl_toast').html();
  45. },
  46. bind: function () {
  47. $('#container').on('click', '#showToast', function () {
  48. $('#toast').show();
  49. setTimeout(function () {
  50. $('#toast').hide();
  51. }, 2000);
  52. }).on('click', '#showLoadingToast', function () {
  53. $('#loadingToast').show();
  54. setTimeout(function () {
  55. $('#loadingToast').hide();
  56. }, 2000);
  57. });
  58. }
  59. };
  60. // dialog
  61. var dialog = {
  62. url: '/dialog',
  63. className: 'dialog',
  64. render: function () {
  65. return $('#tpl_dialog').html();
  66. },
  67. bind: function () {
  68. $('#container').on('click', '#showDialog1', function () {
  69. $('#dialog1').show().on('click', '.weui_btn_dialog', function () {
  70. $('#dialog1').off('click').hide();
  71. });
  72. }).on('click', '#showDialog2', function () {
  73. $('#dialog2').show().on('click', '.weui_btn_dialog', function () {
  74. $('#dialog2').off('click').hide();
  75. });
  76. });
  77. }
  78. };
  79. // progress
  80. var progress = {
  81. url: '/progress',
  82. className: 'progress',
  83. render: function () {
  84. return $('#tpl_progress').html();
  85. },
  86. bind: function () {
  87. $('#container').on('click', '#btnStartProgress', function () {
  88. if ($(this).hasClass('weui_btn_disabled')) {
  89. return;
  90. }
  91. $(this).addClass('weui_btn_disabled');
  92. var progress = 0;
  93. var $progress = $('.js_progress');
  94. function next() {
  95. $progress.css({width: progress + '%'});
  96. progress = ++progress % 100;
  97. setTimeout(next, 30);
  98. }
  99. next();
  100. });
  101. }
  102. };
  103. // msg
  104. var msg = {
  105. url: '/msg',
  106. className: 'msg',
  107. render: function () {
  108. return $('#tpl_msg').html();
  109. }
  110. };
  111. // article
  112. var article = {
  113. url: '/article',
  114. className: 'article',
  115. render: function () {
  116. return $('#tpl_article').html();
  117. }
  118. };
  119. // actionsheet
  120. var actionsheet = {
  121. url: '/actionsheet',
  122. className: 'actionsheet',
  123. render: function () {
  124. return $('#tpl_actionsheet').html();
  125. },
  126. bind: function () {
  127. $('#container').on('click', '#showActionSheet', function () {
  128. var mask = $('#mask');
  129. var weuiActionsheet = $('#weui_actionsheet');
  130. weuiActionsheet.addClass('weui_actionsheet_toggle');
  131. mask.show()
  132. .focus()//加focus是为了触发一次页面的重排(reflow or layout thrashing),使mask的transition动画得以正常触发
  133. .addClass('weui_fade_toggle').one('click', function () {
  134. hideActionSheet(weuiActionsheet, mask);
  135. });
  136. $('#actionsheet_cancel').one('click', function () {
  137. hideActionSheet(weuiActionsheet, mask);
  138. });
  139. mask.unbind('transitionend').unbind('webkitTransitionEnd');
  140. function hideActionSheet(weuiActionsheet, mask) {
  141. weuiActionsheet.removeClass('weui_actionsheet_toggle');
  142. mask.removeClass('weui_fade_toggle');
  143. mask.on('transitionend', function () {
  144. mask.hide();
  145. }).on('webkitTransitionEnd', function () {
  146. mask.hide();
  147. })
  148. }
  149. });
  150. }
  151. };
  152. // icons
  153. var icons = {
  154. url: '/icons',
  155. className: 'icons',
  156. render: function () {
  157. return $('#tpl_icons').html();
  158. }
  159. };
  160. // panel
  161. var panel = {
  162. url: '/panel',
  163. className: 'panel',
  164. render: function () {
  165. return $('#tpl_panel').html();
  166. }
  167. };
  168. // tab
  169. var tab = {
  170. url: '/tab',
  171. className: 'tab',
  172. render: function () {
  173. return $('#tpl_tab').html();
  174. }
  175. };
  176. // navbar
  177. var navbar = {
  178. url: '/navbar',
  179. className: 'navbar',
  180. render: function () {
  181. return $('#tpl_navbar').html();
  182. },
  183. bind: function () {
  184. $('#container').on('click', '.weui_navbar_item', function () {
  185. $(this).addClass('weui_bar_item_on').siblings('.weui_bar_item_on').removeClass('weui_bar_item_on');
  186. });
  187. }
  188. };
  189. // tabbar
  190. var tabbar = {
  191. url: '/tabbar',
  192. className: 'tabbar',
  193. render: function () {
  194. return $('#tpl_tabbar').html();
  195. },
  196. bind: function () {
  197. $('#container').on('click', '.weui_tabbar_item', function () {
  198. $(this).addClass('weui_bar_item_on').siblings('.weui_bar_item_on').removeClass('weui_bar_item_on');
  199. });
  200. }
  201. };
  202. // searchbar
  203. var searchbar = {
  204. url: '/searchbar',
  205. className: 'searchbar',
  206. render: function () {
  207. return $('#tpl_searchbar').html();
  208. },
  209. bind: function () {
  210. $('#container').on('focus', '#search_input', function () {
  211. var $weuiSearchBar = $('#search_bar');
  212. $weuiSearchBar.addClass('weui_search_focusing');
  213. }).on('blur', '#search_input', function () {
  214. var $weuiSearchBar = $('#search_bar');
  215. $weuiSearchBar.removeClass('weui_search_focusing');
  216. if ($(this).val()) {
  217. $('#search_text').hide();
  218. } else {
  219. $('#search_text').show();
  220. }
  221. }).on('input', '#search_input', function () {
  222. var $searchShow = $("#search_show");
  223. if ($(this).val()) {
  224. $searchShow.show();
  225. } else {
  226. $searchShow.hide();
  227. }
  228. }).on('touchend', '#search_cancel', function () {
  229. $("#search_show").hide();
  230. $('#search_input').val('');
  231. }).on('touchend', '#search_clear', function () {
  232. $("#search_show").hide();
  233. $('#search_input').val('');
  234. });
  235. }
  236. };
  237. router.push(home)
  238. .push(button)
  239. .push(cell)
  240. .push(toast)
  241. .push(dialog)
  242. .push(progress)
  243. .push(msg)
  244. .push(article)
  245. .push(actionsheet)
  246. .push(icons)
  247. .push(panel)
  248. .push(tab)
  249. .push(navbar)
  250. .push(tabbar)
  251. .push(searchbar)
  252. .setDefault('/')
  253. .init();
  254. // .container 设置了 overflow 属性, 导致 Android 手机下输入框获取焦点时, 输入法挡住输入框的 bug
  255. // 相关 issue: https://github.com/weui/weui/issues/15
  256. // 解决方法:
  257. // 0. .container 去掉 overflow 属性, 但此 demo 下会引发别的问题
  258. // 1. 参考 http://stackoverflow.com/questions/23757345/android-does-not-correctly-scroll-on-input-focus-if-not-body-element
  259. // Android 手机下, input 或 textarea 元素聚焦时, 主动滚一把
  260. if (/Android/gi.test(navigator.userAgent)) {
  261. window.addEventListener('resize', function () {
  262. if (document.activeElement.tagName == 'INPUT' || document.activeElement.tagName == 'TEXTAREA') {
  263. window.setTimeout(function () {
  264. document.activeElement.scrollIntoViewIfNeeded();
  265. }, 0);
  266. }
  267. })
  268. }
  269. });