【原生JS外掛程式】LoadingBar頁面頂部載入進度條

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

先展示一下已經實現的效果:

 

看到手機上的瀏覽器內建了頁面的載入進度條,想用在pc上。

網上搜了一下,看到幾種頁面loading的方法:

1.在body頭部加入loading元素,在body頁尾寫入指令碼讓loading元素消失。

2.基於jquery,在頁面的不同位置插入指令碼,設定捲軸的寬度。

簡單分析一下:

第一個明顯不是我想要的。

第二個要在body前載入jquery,然後還要使用到jquery的動畫方法,效能肯定達不到最優的狀態。

自己的解決方案:原生JS+css3

上面的方法2其實是可以使用的方法,但是我不想在頁面前載入jquery,怎麼辦?

很簡單,自己用原生的方法寫一個。

給元素添加css3的動畫屬性,讓他能在改變寬度的時候有動畫效果。

transition:all 1s;-moz-transition:all 1s;-webkit-transition:all 1s;-o-transition:all 1s;

在頁面插入一段style,裡面有元素的css和一個css3動畫暫停類

.animation_paused{-webkit-animation-play-state:paused;-moz-animation-play-state:paused;-ms-animation-play-state:paused;animation-play-state:paused;}

 

然後在頁面裡不同的位置調用方法,設定捲軸的寬度即可,需要注意的是方法的引用要在<head></head>裡

<div id="top"></div><script>LoadingBar.setWidth(1)</script><div id="nav"></div><script>LoadingBar.setWidth(20)</script><div id="banner"></div><script>LoadingBar.setWidth(40)</script><div id="main"></div><script>LoadingBar.setWidth(60)</script><div id="right"></div><script>LoadingBar.setWidth(90)</script><div id="foot"></div><script>LoadingBar.setWidth(100)</script>

 

外掛程式源碼:

/*========================================================================LoadingBar 頁面載入進度條@auther LiuMing@blog http://www.cnblogs.com/dtdxrkdemo 在body裡填寫需要載入的進度LoadingBar.setWidth(number)========================================================================*/var LoadingBar = {    /*初始化*/    init:function(){        this.creatStyle();        this.creatLoadDiv();    },    /*記錄當前寬度*/    width:0,    /*頁面裡LoadingBar div*/    oLoadDiv : false,    /*開始*/    setWidth : function(w){        if(!this.oLoadDiv){this.init();}        var oLoadDiv = this.oLoadDiv,            width = Number(w) || 100;        /*防止後面寫入的width小於前面寫入的width*/        (width<this.width) ? width=this.width : this.width = width;        oLoadDiv.className = ‘animation_paused‘;        oLoadDiv.style.width = width + ‘%‘;        oLoadDiv.className = ‘‘;        if(width === 100){this.over(oLoadDiv);}    },    /*頁面載入完畢*/    over : function(obj){        setTimeout(function(){            obj.style.display = ‘none‘;        },1000);    },    /*建立load條*/    creatLoadDiv : function(){        var div = document.createElement(‘div‘);        div.id = ‘LoadingBar‘;        document.body.appendChild(div);        this.oLoadDiv = document.getElementById(‘LoadingBar‘);    },    /*建立style*/    creatStyle : function(){        var nod = document.createElement(‘style‘),               str = ‘#LoadingBar{transition:all 1s;-moz-transition:all 1s;-webkit-transition:all 1s;-o-transition:all 1s;background-color:#f90;height: 3px;width:0; position: fixed;top: 0;z-index: 99999;left: 0;font-size: 0;z-index:9999999;_position:absolute;_top:expression(eval(document.documentElement.scrollTop));}.animation_paused{-webkit-animation-play-state:paused;-moz-animation-play-state:paused;-ms-animation-play-state:paused;animation-play-state:paused;};‘        nod.type = ‘text/css‘;        //ie下        nod.styleSheet ? nod.styleSheet.cssText = str : nod.innerHTML = str;         document.getElementsByTagName(‘head‘)[0].appendChild(nod);     }}

 

【原生JS外掛程式】LoadingBar頁面頂部載入進度條

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.