Skip to content

Commit 5a63642

Browse files
author
zhangxinxu
committed
fix many bugs
1. iOS safari won't slide bug 2. same history bug 3. iOS7+ swipe prev/next page cause slide problem
1 parent 0795c1a commit 5a63642

File tree

3 files changed

+71
-39
lines changed

3 files changed

+71
-39
lines changed

src/mobilebone.css

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,38 +91,39 @@ html, body, .page {
9191
animation-name: slideinfromleft;
9292
}
9393

94+
/* keyframes for slidein from sides */
9495
@-webkit-keyframes slideinfromright {
9596
from { -webkit-transform: translate3d(100%,0,0); }
9697
to { -webkit-transform: translate3d(0,0,0); }
9798
}
9899
@keyframes slideinfromright {
99-
from { transform: translate3d(100%,0,0); }
100-
to { transform: translate3d(0,0,0); }
100+
from { transform: translateX(100%); }
101+
to { transform: translateX(0); }
101102
}
102103
@-webkit-keyframes slideinfromleft {
103104
from { -webkit-transform: translate3d(-100%,0,0); }
104105
to { -webkit-transform: translate3d(0,0,0); }
105106
}
106107
@keyframes slideinfromleft {
107-
from { transform: translate3d(-100%,0,0); }
108-
to { transform: translate3d(0,0,0); }
108+
from { transform: translateX(-100%); }
109+
to { transform: translateX(0); }
109110
}
110111
/* keyframes for slideout to sides */
111112
@-webkit-keyframes slideouttoleft {
112113
from { -webkit-transform: translate3d(0,0,0); }
113114
to { -webkit-transform: translate3d(-100%,0,0); }
114115
}
115116
@keyframes slideouttoleft {
116-
from { transform: translate3d(0,0,0); }
117-
to { transform: translate3d(-100%,0,0); }
117+
from { transform: translateX(0); }
118+
to { transform: translateX(-100%); }
118119
}
119120
@-webkit-keyframes slideouttoright {
120121
from { -webkit-transform: translate3d(0,0,0); }
121122
to { -webkit-transform: translate3d(100%,0,0); }
122123
}
123124
@keyframes slideouttoright {
124-
from { transform: translate3d(0,0,0); }
125-
to { transform: translate3d(100%,0,0); }
125+
from { transform: translateX(0); }
126+
to { transform: translateX(100%); }
126127
}
127128

128129
/* chrysanthemum loading effect */

src/mobilebone.js

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -238,18 +238,22 @@
238238
if (pageOut != null && pageOut.classList) {
239239
// do transition if there are no 'prevent'
240240
if (isPreventOut != true) {
241-
pageOut.style.display = "block";
242241
pageOut.classList.add("out");
243-
pageOut.classList.add(params_out.form);
242+
//if (options.animate !== false) {
243+
pageOut.classList.add(params_out.form);
244+
//}
244245
pageOut.classList.remove("in");
245246
// if reverse direction
246247
pageOut.classList[back? "add": "remove"]("reverse");
248+
pageOut.style.display = "block";
249+
247250
// do fallback every time
248251
var fallback = params_out.fallback;
249252
if (typeof fallback == "string") fallback = params_out.root[fallback];
250253
if (typeof fallback == "function") fallback.call(params_out.root, pageInto, pageOut, options);
251254
}
252255
}
256+
253257
if (pageInto != null && pageInto.classList) {
254258
// for title change
255259
var title = params_in.title,
@@ -267,34 +271,30 @@
267271
// set data-title for first visibie page
268272
pageInto.setAttribute("data-title", document.title);
269273
}
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+
283275
// delete page with same id when options.remove !== false
284276
var pageid = options.id || pageInto.id;
285277

286278
if (options.remove !== false && store[pageid] && store[pageid] != pageInto && store[pageid].parentElement) {
287279
store[pageid].parentElement.removeChild(store[pageid]);
288280
delete store[pageid];
289281
}
290-
282+
291283
// 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
293289
pageInto.classList.remove("out");
294290
pageInto.classList.add("in");
295-
pageOut && pageInto.classList.add(params_in.form);
291+
296292
// if reverse direction
297293
pageInto.classList[back? "add": "remove"]("reverse");
294+
pageInto.style.display = "block";
295+
296+
297+
//console.log(pageInto.className);
298298

299299
// do callback when come in first time
300300
var onpagefirstinto = params_in.onpagefirstinto;
@@ -347,16 +347,15 @@
347347

348348
if (supportHistory && this.pushStateEnabled && options.history !== false && url_push
349349
// hash should be different
350+
// can fix issues #79, #87 maybe
350351
&& url_push_replaced != location.hash
351352
) {
352353
// don't trigger 'popstate' events
353354
history.popstate = false;
354355
// if only pageIn, use 'replaceState'
355356
history[pageOut? "pushState": "replaceState"](null, document.title, url_push.replace(/^#/, "#&"));
356357
}
357-
// reset to popable state
358-
history.popstate = true;
359-
358+
360359
// store page-id, just once
361360
if (!store[pageid]) {
362361
store[pageid] = pageInto;
@@ -367,6 +366,13 @@
367366

368367
if (typeof callback == "string") callback = params_in.root[callback];
369368
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);
370376
}
371377
};
372378

@@ -861,15 +867,27 @@
861867
});
862868
}
863869
}
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+
865883
// change flag-var for avoiding repeat init
866884
hasInited = true;
867885
};
868886

869887
/**
870888
* If 'a' element has href, slide auto when tapping~
871889
**/
872-
Mobilebone.handleTapEvent = function(event) {
890+
Mobilebone.handleTapEvent = function(event) {
873891
/**
874892
// iscroll(set tap: true) may cause twice tap problem
875893
// which is none of Mobilebone's business
@@ -896,7 +914,7 @@
896914
var self_page = document.querySelector(".in." + Mobilebone.classPage);
897915

898916
if (self_page == null || !target) return;
899-
917+
900918
// optional params for Mobilebone.transition
901919
var options = {
902920
target: target
@@ -950,6 +968,8 @@
950968
// 3. cros, or not capture (except data-ajax="true")
951969
if (!href) return;
952970

971+
href = href.replace("#&", "#");
972+
953973
if (target.getAttribute("href").replace(/#/g, "") === "") {
954974
event.preventDefault();
955975
return;
@@ -1049,7 +1069,11 @@
10491069
* page change when history change
10501070
**/
10511071
window.addEventListener("popstate", function() {
1052-
1072+
if (history.popstateswipe == true) {
1073+
location.reload();
1074+
history.popstateswipe = false;
1075+
return;
1076+
}
10531077
if (history.popstate == false) {
10541078
history.popstate = true;
10551079
return;
@@ -1090,6 +1114,7 @@
10901114
});
10911115
}
10921116
});
1093-
1117+
10941118
return Mobilebone;
10951119
});
1120+

test/base-slide/index.html

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@
1111
<body>
1212
<div id="pageHome" class="page out">
1313
<ul>
14-
<li><a href="#page1">页面1</a></li>
15-
<li><a href="#page2">页面2</a></li>
14+
<li><a href="#&page1">页面1-#&amp;page1</a></li>
15+
<li><a href="#page2">页面2-#page2</a></li>
1616
<li><a href="#page3">页面3</a></li>
1717
</ul>
1818
<ul>
1919
<li><a href="../index.html" data-ajax="false">&laquo; 返回测试引导首页</a></li>
20+
<li id="result"></li>
2021
</ul>
2122
</div>
22-
<div id="page1" class="page out"><a href="#pageHome" data-rel="back">&laquo;返回1</a></div>
23-
<div id="page2" class="page out"><a href="#pageHome" data-rel="back">&laquo;返回2</a></div>
24-
<div id="page3" class="page out"><a href="#pageHome" data-rel="back">&laquo;返回3</a></div>
23+
<div id="page1" class="page out"><a href="#&pageHome" data-rel="back">&laquo;返回1</a></div>
24+
<div id="page2" class="page out"><a href="#&pageHome" data-rel="back">&laquo;返回2</a></div>
25+
<div id="page3" class="page out"><a href="#&pageHome" data-rel="back">&laquo;返回3</a></div>
2526

2627
<script src="../../src/mobilebone.js"></script>
28+
<script>
29+
console.log = function(content) {
30+
document.getElementById("result").innerHTML += '<p>'+ content +'</p>';
31+
};
32+
</script>
2733
</body>
2834
</html>

0 commit comments

Comments
 (0)