在页面样式加载完成之前,自定义一个等待页面,防止页面变化太突兀

/**
 * 未加载完成CSS时,显示当前页面
 * 此文件放在<head>结尾即可
 * */

//获取浏览器页面可见高度和宽度
var _PageHeight = document.documentElement.clientHeight,
    _PageWidth = document.documentElement.clientWidth;
//计算loading框距离顶部和左部的距离(loading框的宽度为215px,高度为61px)
var _LoadingTop = _PageHeight > 61 ? (_PageHeight - 61) / 2 : 0,
    _LoadingLeft = _PageWidth > 215 ? (_PageWidth - 215) / 2 : 0;
//在页面未加载完毕之前显示的loading Html自定义内容
var _LoadingHtml = '<div id="loadingDiv" style="position:absolute;left:0;width:100%;height:' + _PageHeight + 'px;top:0;background:#f3f8ff;opacity:1;filter:alpha(opacity=80);z-index:10000;"><div style="position: absolute; cursor1: wait; left: ' + _LoadingLeft + 'px; top:' + _LoadingTop + 'px; width: auto; height: 40px; line-height: 40px; padding-left: 10px; padding-right: 10px; background: #fff url(Image/loading.gif) no-repeat scroll 5px 10px; border: 1px solid #95B8E7; color: #696969; font-size:13px;font-family:\'Microsoft YaHei\';">页面加载中,请等待...</div></div>';
//呈现loading效果
document.write(_LoadingHtml);

//监听加载状态改变
document.onreadystatechange = completeLoading;

//加载状态为complete时移除loading效果
function completeLoading() {
    if (document.readyState == "complete") {
        var loadingMask = document.getElementById('loadingDiv');
        loadingMask.parentNode.removeChild(loadingMask);
    }
}

将此代码拷到js文件里,然后在<head>结尾引用即可

/** * 未加载完成CSS时,显示当前页面 * 此文件放在<head>结尾即可 * */
//获取浏览器页面可见高度和宽度var _PageHeight = document.documentElement.clientHeight,    _PageWidth = document.documentElement.clientWidth;//计算loading框距离顶部和左部的距离(loading框的宽度为215px,高度为61px)var _LoadingTop = _PageHeight > 61 ? (_PageHeight - 61) / 2 : 0,    _LoadingLeft = _PageWidth > 215 ? (_PageWidth - 215) / 2 : 0;//在页面未加载完毕之前显示的loading Html自定义内容var _LoadingHtml = '<div id="loadingDiv" style="position:absolute;left:0;width:100%;height:' + _PageHeight + 'px;top:0;background:#f3f8ff;opacity:1;filter:alpha(opacity=80);z-index:10000;"><div style="position: absolute; cursor1: wait; left: ' + _LoadingLeft + 'px; top:' + _LoadingTop + 'px; width: auto; height: 40px; line-height: 40px; padding-left: 10px; padding-right: 10px; background: #fff url(Image/loading.gif) no-repeat scroll 5px 10px; border: 1px solid #95B8E7; color: #696969; font-size:13px;font-family:\'Microsoft YaHei\';">页面加载中,请等待...</div></div>';//呈现loading效果document.write(_LoadingHtml);
//监听加载状态改变document.onreadystatechange = completeLoading;
//加载状态为complete时移除loading效果function completeLoading() {    if (document.readyState == "complete") {        var loadingMask = document.getElementById('loadingDiv');        loadingMask.parentNode.removeChild(loadingMask);    }}

posted @ 2020-03-02 16:07  sevene  阅读(469)  评论(0编辑  收藏  举报