H5+JS implements page loading animation

青灯夜游
Release: 2021-01-27 19:12:14
forward
5247 people have browsed it

This article will introduce to you how to implement page loading animation in HTML5 JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

H5+JS implements page loading animation

(Related tutorial recommendations: html tutorial)

1. Use a timer and wait every time.

2. Determine whether the loading animation will exit based on whether the page loading is completed.

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

3. Both of the above are directly using gif images. In order to make the web page load faster, use css3 to create playback animations

<!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进度条</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>
Copy after login

The effect is as follows

H5+JS implements page loading animation

4. Based on the page loading progress in real time, the loading percentage is displayed

<!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>实时获取加载数据的进度条</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>
Copy after login

The effect is as follows. Because the loading is too fast, a screenshot of the initial page of the animation is taken.

H5+JS implements page loading animation

The style code written can be found at this URL, http://autoprefixer.github.io/ to help us automatically adapt to different browsers.

H5+JS implements page loading animation

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of H5+JS implements page loading animation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template