登录  /  注册

H5+JS实现页面加载动画

青灯夜游
发布: 2021-01-27 19:12:14
转载
4962人浏览过

本篇文章给大家介绍一下html5+javascript实现页面加载动画的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

H5+JS实现页面加载动画

(相关教程推荐:html教程 )

1.使用定时器,每次都要等待。

2.根据页面加载是否完成,来判断加载动画是否退出。

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

3.以上两个都是直接使用gif图,为了使网页加载更快,使用css3来制作播放动画

<!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>
登录后复制

效果如下

1.png

4.实时根据页面加载进度,显示加载百分百

<!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>
登录后复制

效果如下,由于加载过快,就截图动画初始页面。

2.png

编写的style代码可以到这个网址,http://autoprefixer.github.io/  帮助我们自动适配不同的游览器。

3.png

更多编程相关知识,请访问:编程视频!!

以上就是H5+JS实现页面加载动画的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:博客园网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号