Skip to content

Commit ae36c6e

Browse files
committed
Merge pull request #88 from zhangxinxu/develop
Develop
2 parents c396f46 + 97a3877 commit ae36c6e

File tree

5 files changed

+78
-44
lines changed

5 files changed

+78
-44
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mobilebone",
3-
"version": "2.3.3",
3+
"version": "2.4.0",
44
"description": "Bone main for mobile web APP with a sigle page mode.",
55
"main": "src/mobilebone.js",
66
"directories": {

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: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*
3939
* @type string
4040
**/
41-
Mobilebone.VERSION = '2.3.3';
41+
Mobilebone.VERSION = '2.4.0';
4242

4343
/**
4444
* Whether catch attribute of href from element with tag 'a'
@@ -238,18 +238,23 @@
238238
if (pageOut != null && pageOut.classList) {
239239
// do transition if there are no 'prevent'
240240
if (isPreventOut != true) {
241+
pageOut.classList.add(params_out.form);
242+
// reflow
243+
pageOut.offsetWidth = pageOut.offsetWidth;
244+
// go, go, go
241245
pageOut.style.display = "block";
242246
pageOut.classList.add("out");
243-
pageOut.classList.add(params_out.form);
244247
pageOut.classList.remove("in");
245248
// if reverse direction
246249
pageOut.classList[back? "add": "remove"]("reverse");
250+
247251
// do fallback every time
248252
var fallback = params_out.fallback;
249253
if (typeof fallback == "string") fallback = params_out.root[fallback];
250254
if (typeof fallback == "function") fallback.call(params_out.root, pageInto, pageOut, options);
251255
}
252256
}
257+
253258
if (pageInto != null && pageInto.classList) {
254259
// for title change
255260
var title = params_in.title,
@@ -267,35 +272,27 @@
267272
// set data-title for first visibie page
268273
pageInto.setAttribute("data-title", document.title);
269274
}
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-
275+
283276
// delete page with same id when options.remove !== false
284277
var pageid = options.id || pageInto.id;
285278

286279
if (options.remove !== false && store[pageid] && store[pageid] != pageInto && store[pageid].parentElement) {
287280
store[pageid].parentElement.removeChild(store[pageid]);
288281
delete store[pageid];
289282
}
290-
283+
291284
// do transition
285+
if (pageOut) pageInto.classList.add(params_in.form);
286+
// iOS bug
287+
// reflow for fixing issues #80, #86
288+
pageInto.offsetWidth = pageInto.offsetWidth;
289+
// go~ as normal
292290
pageInto.style.display = "block";
293291
pageInto.classList.remove("out");
294292
pageInto.classList.add("in");
295-
pageOut && pageInto.classList.add(params_in.form);
296293
// if reverse direction
297294
pageInto.classList[back? "add": "remove"]("reverse");
298-
295+
299296
// do callback when come in first time
300297
var onpagefirstinto = params_in.onpagefirstinto;
301298
if (!store[pageid]) {
@@ -339,20 +336,23 @@
339336
});
340337

341338
// history
342-
var url_push = pageid;
339+
var url_push = pageid, url_push_replaced = '';
343340
if (url_push && /^#/.test(url_push) == false) {
344341
url_push = "#" + url_push;
345342
}
346-
347-
if (supportHistory && this.pushStateEnabled && options.history !== false && url_push) {
343+
url_push_replaced = url_push.replace(/^#/, "#&");
344+
345+
if (supportHistory && this.pushStateEnabled && options.history !== false && url_push
346+
// hash should be different
347+
// can fix issues #79, #87 maybe
348+
&& url_push_replaced != location.hash
349+
) {
348350
// don't trigger 'popstate' events
349351
history.popstate = false;
350352
// if only pageIn, use 'replaceState'
351353
history[pageOut? "pushState": "replaceState"](null, document.title, url_push.replace(/^#/, "#&"));
352354
}
353-
// reset to popable state
354-
history.popstate = true;
355-
355+
356356
// store page-id, just once
357357
if (!store[pageid]) {
358358
store[pageid] = pageInto;
@@ -363,6 +363,13 @@
363363

364364
if (typeof callback == "string") callback = params_in.root[callback];
365365
if (typeof callback == "function") callback.call(params_in.root, pageInto, pageOut, options);
366+
367+
// Safari do 'popstate' after 'pushState/replaceState'
368+
// So, we neet setTimeout to avoid excuting 'Mobilebone.transition()' twice
369+
setTimeout(function() {
370+
// reset to popable state
371+
history.popstate = true;
372+
}, 17);
366373
}
367374
};
368375

@@ -857,15 +864,27 @@
857864
});
858865
}
859866
}
860-
867+
// Important:
868+
// In ios7+, swipe the edge of page will navigate Safari
869+
// that will trigger 'popstate' events and the page will transition twice
870+
var isSafari7 = !!navigator.userAgent.match(/safari/i) && !navigator.userAgent.match(/chrome/i) && typeof document.hidden !== "undefined" && !window.chrome;
871+
if ('ontouchstart' in window == true && isSafari7) {
872+
document.addEventListener("touchmove", function() {
873+
history.popstateswipe = true;
874+
});
875+
document.addEventListener("touchend", function() {
876+
history.popstateswipe = false;
877+
});
878+
}
879+
861880
// change flag-var for avoiding repeat init
862881
hasInited = true;
863882
};
864883

865884
/**
866885
* If 'a' element has href, slide auto when tapping~
867886
**/
868-
Mobilebone.handleTapEvent = function(event) {
887+
Mobilebone.handleTapEvent = function(event) {
869888
/**
870889
// iscroll(set tap: true) may cause twice tap problem
871890
// which is none of Mobilebone's business
@@ -892,7 +911,7 @@
892911
var self_page = document.querySelector(".in." + Mobilebone.classPage);
893912

894913
if (self_page == null || !target) return;
895-
914+
896915
// optional params for Mobilebone.transition
897916
var options = {
898917
target: target
@@ -946,6 +965,8 @@
946965
// 3. cros, or not capture (except data-ajax="true")
947966
if (!href) return;
948967

968+
href = href.replace("#&", "#");
969+
949970
if (target.getAttribute("href").replace(/#/g, "") === "") {
950971
event.preventDefault();
951972
return;
@@ -1045,7 +1066,11 @@
10451066
* page change when history change
10461067
**/
10471068
window.addEventListener("popstate", function() {
1048-
1069+
if (history.popstateswipe == true) {
1070+
location.reload();
1071+
history.popstateswipe = false;
1072+
return;
1073+
}
10491074
if (history.popstate == false) {
10501075
history.popstate = true;
10511076
return;
@@ -1086,6 +1111,7 @@
10861111
});
10871112
}
10881113
});
1089-
1114+
10901115
return Mobilebone;
10911116
});
1117+

test/ajax-html/c.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
<body>
88
<div class="page out">
99
<ul>
10-
<li><a href="#pageHome" data-rel="back">c页面,直接刷新,浏览器返回</a></li>
10+
<li><a href="#pageHome" data-rel="back">c页面,浏览器返回</a></li>
11+
<li><a href="" data-rel="back">再次加重c页面</a></li>
1112
<li><a href="javascript:" data-rel="back">直接返回b页面</a></li>
1213
</ul>
1314
</div>

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)