|
238 | 238 | if (pageOut != null && pageOut.classList) {
|
239 | 239 | // do transition if there are no 'prevent'
|
240 | 240 | if (isPreventOut != true) {
|
241 |
| - pageOut.style.display = "block"; |
242 | 241 | pageOut.classList.add("out");
|
243 |
| - pageOut.classList.add(params_out.form); |
| 242 | + //if (options.animate !== false) { |
| 243 | + pageOut.classList.add(params_out.form); |
| 244 | + //} |
244 | 245 | pageOut.classList.remove("in");
|
245 | 246 | // if reverse direction
|
246 | 247 | pageOut.classList[back? "add": "remove"]("reverse");
|
| 248 | + pageOut.style.display = "block"; |
| 249 | + |
247 | 250 | // do fallback every time
|
248 | 251 | var fallback = params_out.fallback;
|
249 | 252 | if (typeof fallback == "string") fallback = params_out.root[fallback];
|
250 | 253 | if (typeof fallback == "function") fallback.call(params_out.root, pageInto, pageOut, options);
|
251 | 254 | }
|
252 | 255 | }
|
| 256 | + |
253 | 257 | if (pageInto != null && pageInto.classList) {
|
254 | 258 | // for title change
|
255 | 259 | var title = params_in.title,
|
|
267 | 271 | // set data-title for first visibie page
|
268 | 272 | pageInto.setAttribute("data-title", document.title);
|
269 | 273 | }
|
270 |
| - // Fastclick may cause slide bug in iOS8, any innerHTML change can fix it! |
271 |
| - // issues #80 |
272 |
| - //if (typeof FastClick != "undefined") { |
273 |
| - var mobilebone = document.querySelector("mobilebone"); |
274 |
| - if (mobilebone == null) { |
275 |
| - mobilebone = document.createElement("mobilebone"); |
276 |
| - mobilebone.style.position = "absolute"; |
277 |
| - mobilebone.style.clip = "rect(0 0 0 0)"; |
278 |
| - document.body.appendChild(mobilebone); |
279 |
| - } |
280 |
| - mobilebone.innerHTML = mobilebone.innerHTML.replace('11', '') + '1'; |
281 |
| - //} |
282 |
| - |
| 274 | + |
283 | 275 | // delete page with same id when options.remove !== false
|
284 | 276 | var pageid = options.id || pageInto.id;
|
285 | 277 |
|
286 | 278 | if (options.remove !== false && store[pageid] && store[pageid] != pageInto && store[pageid].parentElement) {
|
287 | 279 | store[pageid].parentElement.removeChild(store[pageid]);
|
288 | 280 | delete store[pageid];
|
289 | 281 | }
|
290 |
| - |
| 282 | + |
291 | 283 | // do transition
|
292 |
| - pageInto.style.display = "block"; |
| 284 | + if (pageOut) pageInto.classList.add(params_in.form); |
| 285 | + // iOS bug |
| 286 | + // reflow for fixing issues #80, #86 |
| 287 | + pageInto.offsetWidth = pageInto.offsetWidth; |
| 288 | + // go~ as normal |
293 | 289 | pageInto.classList.remove("out");
|
294 | 290 | pageInto.classList.add("in");
|
295 |
| - pageOut && pageInto.classList.add(params_in.form); |
| 291 | + |
296 | 292 | // if reverse direction
|
297 | 293 | pageInto.classList[back? "add": "remove"]("reverse");
|
| 294 | + pageInto.style.display = "block"; |
| 295 | + |
| 296 | + |
| 297 | + //console.log(pageInto.className); |
298 | 298 |
|
299 | 299 | // do callback when come in first time
|
300 | 300 | var onpagefirstinto = params_in.onpagefirstinto;
|
|
347 | 347 |
|
348 | 348 | if (supportHistory && this.pushStateEnabled && options.history !== false && url_push
|
349 | 349 | // hash should be different
|
| 350 | + // can fix issues #79, #87 maybe |
350 | 351 | && url_push_replaced != location.hash
|
351 | 352 | ) {
|
352 | 353 | // don't trigger 'popstate' events
|
353 | 354 | history.popstate = false;
|
354 | 355 | // if only pageIn, use 'replaceState'
|
355 | 356 | history[pageOut? "pushState": "replaceState"](null, document.title, url_push.replace(/^#/, "#&"));
|
356 | 357 | }
|
357 |
| - // reset to popable state |
358 |
| - history.popstate = true; |
359 |
| - |
| 358 | + |
360 | 359 | // store page-id, just once
|
361 | 360 | if (!store[pageid]) {
|
362 | 361 | store[pageid] = pageInto;
|
|
367 | 366 |
|
368 | 367 | if (typeof callback == "string") callback = params_in.root[callback];
|
369 | 368 | if (typeof callback == "function") callback.call(params_in.root, pageInto, pageOut, options);
|
| 369 | + |
| 370 | + // Safari do 'popstate' after 'pushState/replaceState' |
| 371 | + // So, we neet setTimeout to avoid excuting 'Mobilebone.transition()' twice |
| 372 | + setTimeout(function() { |
| 373 | + // reset to popable state |
| 374 | + history.popstate = true; |
| 375 | + }, 17); |
370 | 376 | }
|
371 | 377 | };
|
372 | 378 |
|
|
861 | 867 | });
|
862 | 868 | }
|
863 | 869 | }
|
864 |
| - |
| 870 | + // Important: |
| 871 | + // In ios7+, swipe the edge of page will navigate Safari |
| 872 | + // that will trigger 'popstate' events and the page will transition twice |
| 873 | + var isSafari7 = !!navigator.userAgent.match(/safari/i) && !navigator.userAgent.match(/chrome/i) && typeof document.hidden !== "undefined" && !window.chrome; |
| 874 | + if ('ontouchstart' in window == true) { |
| 875 | + document.addEventListener("touchmove", function() { |
| 876 | + history.popstateswipe = true; |
| 877 | + }); |
| 878 | + document.addEventListener("touchend", function() { |
| 879 | + history.popstateswipe = false; |
| 880 | + }); |
| 881 | + } |
| 882 | + |
865 | 883 | // change flag-var for avoiding repeat init
|
866 | 884 | hasInited = true;
|
867 | 885 | };
|
868 | 886 |
|
869 | 887 | /**
|
870 | 888 | * If 'a' element has href, slide auto when tapping~
|
871 | 889 | **/
|
872 |
| - Mobilebone.handleTapEvent = function(event) { |
| 890 | + Mobilebone.handleTapEvent = function(event) { |
873 | 891 | /**
|
874 | 892 | // iscroll(set tap: true) may cause twice tap problem
|
875 | 893 | // which is none of Mobilebone's business
|
|
896 | 914 | var self_page = document.querySelector(".in." + Mobilebone.classPage);
|
897 | 915 |
|
898 | 916 | if (self_page == null || !target) return;
|
899 |
| - |
| 917 | + |
900 | 918 | // optional params for Mobilebone.transition
|
901 | 919 | var options = {
|
902 | 920 | target: target
|
|
950 | 968 | // 3. cros, or not capture (except data-ajax="true")
|
951 | 969 | if (!href) return;
|
952 | 970 |
|
| 971 | + href = href.replace("#&", "#"); |
| 972 | + |
953 | 973 | if (target.getAttribute("href").replace(/#/g, "") === "") {
|
954 | 974 | event.preventDefault();
|
955 | 975 | return;
|
|
1049 | 1069 | * page change when history change
|
1050 | 1070 | **/
|
1051 | 1071 | window.addEventListener("popstate", function() {
|
1052 |
| - |
| 1072 | + if (history.popstateswipe == true) { |
| 1073 | + location.reload(); |
| 1074 | + history.popstateswipe = false; |
| 1075 | + return; |
| 1076 | + } |
1053 | 1077 | if (history.popstate == false) {
|
1054 | 1078 | history.popstate = true;
|
1055 | 1079 | return;
|
|
1090 | 1114 | });
|
1091 | 1115 | }
|
1092 | 1116 | });
|
1093 |
| - |
| 1117 | + |
1094 | 1118 | return Mobilebone;
|
1095 | 1119 | });
|
| 1120 | + |
0 commit comments