我正在开发一个单页应用程序,其中每个部分都是通过 ajax 加载的。主页是它自己的页面,然后单击“关于”,它会将您带到单页应用程序。我已经获得了要更改每个部分的 url 以及浏览器上的后退和前进按钮以使用此代码段:
$('.down-button').on('click', function() {
var stateObj = { principles: "about-principles" };
history.pushState(stateObj, null, "about-principles");
$('#principles-wrap').scrollintoview({ duration: 1000 });
});
但是,当您一路点击返回首页时,首页并没有刷新,您停留在“关于”页面。或者,如果您单击返回主页的链接,则会收到 404 错误。
我错过了什么?
编辑
这是其余的代码:
function button_scroll () {
// Call scrollIntoView js file into this
$.getScript("/js/jquery.scrollintoview.min.js", function() {
$('.down-button').on('click', function() {
var stateObj = { principles: "about-principles" };
history.pushState(stateObj, null, "about-principles");
$('#principles-wrap').scrollintoview({ duration: 1000 });
});
$('.up-button-principles').on('click', function() {
var stateObj = { mission: "about-mission" };
history.pushState(stateObj, null, "about-mission");
$('#mission-wrap').scrollintoview({ duration: 1000 });
});
$('.down-button-principles').on('click', function() {
var stateObj = { snapshot: "about-snapshot" };
history.pushState(stateObj, null, "about-snapshot");
$('#snapshot-wrap').scrollintoview({ duration: 1000 });
});
$('.up-button-snapshot').on('click', function() {
var stateObj = { principles: "about-principles" };
history.pushState(stateObj, null, "about-principles");
$('#principles-wrap').scrollintoview({ duration: 1000 });
});
$('.down-button-snapshot').on('click', function() {
var stateObj = { people: "about-people" };
history.pushState(stateObj, null, "about-people");
$('#people-wrap').scrollintoview({ duration: 1000 });
});
$('.up-button-people').on('click', function() {
var stateObj = { snapshot: "about-snapshot" };
history.pushState(stateObj, null, "about-snapshot");
$('#snapshot-wrap').scrollintoview({ duration: 1000 });
});
});
}
//Dropdown
$(document).ready(function () {
$(window).on('load resize', function () {
resize_mission();
resize_principles();
resize_snapshot();
resize_people();
button_scroll();
});
var dropDown = $('.dropdown');
$('h3.about').on('click', function() {
dropDown.slideDown('fast', function() {
var url = 'about2.php'
var stateObj = { mission: "about-mission" };
history.pushState(stateObj, null, "about-mission");
$.get(url, function(data) {
resize_mission();
resize_principles();
resize_snapshot();
resize_people();
button_scroll();
});
});
});
});
这就是我正在做的。没有插件。