记录日常点点滴滴,欢迎来到我的小站。

0%

jQuery Ajax load pushState构筑无刷新跳转到新页面

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
function nextPage(pageTo) { //pageTo 数组参数代表页面数字
var pagelen = pageTo.length;
var newArr = pageTo.splice(0, pagelen);
console.log(newArr);
setTimeout(function () {
finished = true;
$('html,body').animate({ scrollTop: 0 }, 0);
var ajax,currentState;
var target = "frontier" + newArr[0] + ".html",
url = $(target).attr('href');
!$(this).hasClass('current') && $(this).addClass('current').siblings().removeClass("current");
if (ajax == undefined) {
currentState = {
url: document.location.href,
title: document.title,
html: $('.section-all').html()
};
}
$(this).find("script").appendTo('#content');
console.log($(this).find("script"));
ajax = $('.st_box').load(target + " .section-all", function (response) {
//console.log($($.parseHTML(response)).find('.section-all').html());

document.title = $($.parseHTML(response)).find('#about .section h2').text() + " | キヤノングローバル";
var state = {
url: target,
title: document.title,
html: $('.section-all').html()
};
console.log(state.html);
history.pushState(state, null, target);
});

window.addEventListener('popstate', function (event) {
if (ajax == null) {
return;
} else if (event && event.state) {
document.title = event.state.title;
$('.st_box').html(event.state.html);
} else {
document.title = currentState.title;
$('.st_box').html(currentState.html);
}
});
}, 1000);
}