溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

html5+javascript實(shí)現(xiàn)頁(yè)面加載動(dòng)畫的示例

發(fā)布時(shí)間:2021-01-28 10:29:38 來(lái)源:億速云 閱讀:297 作者:小新 欄目:web開發(fā)

小編給大家分享一下html5+javascript實(shí)現(xiàn)頁(yè)面加載動(dòng)畫的示例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1.使用定時(shí)器,每次都要等待。

2.根據(jù)頁(yè)面加載是否完成,來(lái)判斷加載動(dòng)畫是否退出。

<script>
        document.onreadystatechange=function(){
            console.log(document.readyState);
            if(document.readyState=="complete"){
                $(".loading").fadeOut();
            }
        }        
     </script>

3.以上兩個(gè)都是直接使用gif圖,為了使網(wǎng)頁(yè)加載更快,使用css3來(lái)制作播放動(dòng)畫

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>CSS3進(jìn)度條</title>
    <style>
            .loading{width:100%; height: 100%;position: fixed;top:0;left: 0;z-index: 100;background: #ffffff;}
            .loading .pic{
            width: 64px;
            height: 64px;
            /* background: url(images/loading.gif); */
            position: absolute;
            top: 0;
            bottom: 0;
            left:0;
            right:0;
            margin: auto}
 
            .loading .pic i{
                display: block;
                float: left;
                width: 6px;
                height: 50px;
                background: #399;
                margin: 0 2px;
                transform: scaleY(0.4);
                animation: load 1.2s infinite;
            }
            .loading .pic i:nth-child(1){animation-delay:0.1s }
            .loading .pic i:nth-child(2){animation-delay:0.2s }
            .loading .pic i:nth-child(3){animation-delay:0.3s }
            .loading .pic i:nth-child(4){animation-delay: 0.4s}
            .loading .pic i:nth-child(5){animation-delay:0.5s }
 
            @keyframes load{
                0%,40%,100%{transform: scaleY(0.4)}
                20%{transform:scaleY(1) }
            }
    </style>
</head>
<body>
        
        <div>
            <div>
                <i></i>
                <i></i>
                <i></i>
                <i></i>
                <i></i>
            </div>
        </div>
    <img src="http://i1.hdslb.com/bfs/archive/763293ce06bf1e684ef0ea3da43ae5008d8564b8.jpg" alt=""/>
    <img src="http://i1.hdslb.com/bfs/archive/763293ce06bf1e684ef0ea3da43ae5008d8564b8.jpg" alt=""/>
    <img src="http://i1.hdslb.com/bfs/archive/763293ce06bf1e684ef0ea3da43ae5008d8564b8.jpg" alt=""/>
    <img src="http://i1.hdslb.com/bfs/archive/763293ce06bf1e684ef0ea3da43ae5008d8564b8.jpg" alt=""/>
</body>
</html>

效果如下

html5+javascript實(shí)現(xiàn)頁(yè)面加載動(dòng)畫的示例

4.實(shí)時(shí)根據(jù)頁(yè)面加載進(jìn)度,顯示加載百分百

<!DOCTYPE html>
<html>
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>實(shí)時(shí)獲取加載數(shù)據(jù)的進(jìn)度條</title>
    <style>
        .loading {
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0;
            left: 0;
            z-index: 100;
            background: #ffffff;
        }
 
        .loading .pic {
            width: 100px;
            height: 100px;
            /* background: url(images/loading.gif); */
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
            /* border: 1px solid red; */
            font-size: 30px;
            text-align: center;
            line-height: 100px;
        }
 
        .loading .pic span{
            display: block;
            width: 80px;
            height: 80px;
            position: absolute;
            top:10px;
            left: 10px;
            border-radius: 50%;
            box-shadow: 0 3px 0 #666;
            animation: rotate 1s infinite linear;
        }
        @keyframes rotate{
            0%{transform: rotate(0deg);}
            100%{transform: rotate(360deg);}
        }
            
    </style>
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script>
         $(function(){
             var img=$("img");
             var num=0;
             img.each(function(i){
                 var oImg=new Image();
 
                 oImg.onload=function(){
                     oImg.onload=null;
                     num++;
                     $(".loading b").html(parseInt( num/$("img").size()*100)+"%");
                     if(num>=i){
                         $(".loading").fadeOut();
                     }
                 }
                 oImg.src=img[i].src;
             });       
    })
 
 
    </script>
</head>
 
<body>
    <div>
        <div>
            <span></span>
            <b>0%</b>
        </div>
    </div>
    <img src="http://i1.hdslb.com/bfs/archive/763293ce06bf1e684ef0ea3da43ae5008d8564b8.jpg" alt="" />
    <img src="http://i1.hdslb.com/bfs/archive/763293ce06bf1e684ef0ea3da43ae5008d8564b8.jpg" alt="" />
    <img src="http://i1.hdslb.com/bfs/archive/763293ce06bf1e684ef0ea3da43ae5008d8564b8.jpg" alt="" />
    <img src="http://i1.hdslb.com/bfs/archive/763293ce06bf1e684ef0ea3da43ae5008d8564b8.jpg" alt="" />
</body>
 
</html>

效果如下,由于加載過快,就截圖動(dòng)畫初始頁(yè)面。

html5+javascript實(shí)現(xiàn)頁(yè)面加載動(dòng)畫的示例

編寫的style代碼可以到這個(gè)網(wǎng)址,http://autoprefixer.github.io/  幫助我們自動(dòng)適配不同的游覽器。

html5+javascript實(shí)現(xiàn)頁(yè)面加載動(dòng)畫的示例

以上是“html5+javascript實(shí)現(xiàn)頁(yè)面加載動(dòng)畫的示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI