H_MZ 2017-12-02 15:19 采纳率: 0%
浏览 24

一页网站页面过渡

I have this code, which loads another html file inside the main one, can i ask how can i add transitions when i click on a link that triggers the XMLHTTPRequest, and where do i put it to achieve the effect of a single page site?

Could I add class first to run the animation then process the XMLHTTPRequest or something like that. any suggestions or source?

let main = document.querySelector('body > main');

window.addEventListener('popstate', function (e) {
requestPage(e.state, false);
});
function requestPage (url, push) {
let xhr = new XMLHttpRequest();

xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
        main.innerHTML = xhr.responseText;
        attachLinkClickHandlers(main);

        console.log(xhr.responseText)
        if (push)
            history.pushState(url, document.title, url);
    }
};
xhr.open('get', url, true);
xhr.setRequestHeader('Content-Only', 1);
xhr.send();
}
function attachLinkClickHandlers (parent) {
let links = parent.querySelectorAll('a:not([href^="http"])');

[].forEach.call(links, function (link) {
    link.addEventListener('click', function (e) {
        requestPage(link.href, true);

        e.preventDefault();
        return false;
    });
});
}
attachLinkClickHandlers(document);
history.replaceState(location.href, document.title, location.href);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题