使用HTML+CSS实现网页loading加载效果,支持定时或加载完成后隐藏
网页使用loading可以给用户带来更好的体验,避免网页渲染中长时间出现网页整体空白从而影响访客的体验,loading在部分大型APP也有在应用。
下面使用HTML+CSS+JS实现完整的Loading效果。
请先引入jQuery,因为JS定时隐藏依赖jq。
1.HTML
<div class="loaderbg"><div class="spinner"><div class="double rect1"></div><div class="double rect2"></div><div class="double rect3"></div><div class="double rect4"></div><div class="double rect5"></div></div> </div>
loaderbg类为loading的背景色,为白色。
2.CSS
.loaderbg {background-color: #fff;width: 100%;height: 100%;overflow: hidden;position: fixed;left: 0;top: 0;z-index: 99999999 }::-webkit-scrollbar {width: 7.5px;height: 6px;background-color: #f0f0f0;display: none }::-webkit-scrollbar-thumb {background-color: #b1b1b1;border-radius: 15px }::-webkit-scrollbar-thumb:hover {background-color: #777 }.spinner {position: absolute;top: 50%;left: 50%;margin-left: -25px;margin-top: -30px;width: 50px;height: 60px;text-align: center;font-size: 10px }.spinner>.double {background: #49a9ee;height: 100%;width: 6px;display: inline-block;-webkit-animation: stretchDelay 1.2s infinite ease-in-out;animation: stretchDelay 1.2s infinite ease-in-out }.spinner .rect2 {-webkit-animation-delay: -1.1s;animation-delay: -1.1s }.spinner .rect3 {-webkit-animation-delay: -1.0s;animation-delay: -1.0s }.spinner .rect4 {-webkit-animation-delay: -0.9s;animation-delay: -0.9s }.spinner .rect5 {-webkit-animation-delay: -0.8s;animation-delay: -0.8s }@-webkit-keyframes stretchDelay {0%,40%,100% {-webkit-transform: scaleY(.4)}20% {-webkit-transform: scaleY(1)} }@keyframes stretchDelay {0%,40%,100% {transform: scaleY(.4);-webkit-transform: scaleY(.4)}20% {transform: scaleY(1);-webkit-transform: scaleY(1)} }
3.JS
js在这里的作用为定时或网页加载完成后关闭loaderbg类
//页面加载完成之后隐藏loading $(window).load(function () {$(".loaderbg").hide(); });//设置页面加载3秒之后隐藏loading /*$(function () {setTimeout(function () {$(".loaderbg").hide();alert("页面加载完成啦!");},3000); })*/
第一种方法是等待网页全部加载完成后再隐藏loading,但同时如果网页其他资源文件加载缓慢(如图片等),loading也会随之存在更长时间。
第二中方法是设置定时隐藏loading,可以根据实际需求更改隐藏时间,默认为3s。
建议实际使用时,删除 alert("页面加载完成啦!"); 避免引起用户反感,只做效果测试。
以上第一种方法jquery低版本测试正常,高版本可能会报错:ncaught TypeError: a.indexOf is not a function
at r.fn.load
原因是在jQuery 3.x 中取消了 load(),将 $(window).load(function () { 替换为 $(window).on('load',function(){ 即可,如:
$(window).on('load',function(){$(".loaderbg").hide();});
同时考虑到如果用户的浏览器侧不支持JavaScript或者JavaScript被禁用,需要使用noscript标签,添加display:none属性即可,noscript只会在浏览器环境不支持JS或者JS被禁用才会执行
<noscript><style>.loaderbg {display: none;}</style> </noscript>
4.实例代码:
<!DOCTYPE html> <html lang="zh"> <head><meta charset="UTF-8"><title>使用HTML+CSS实现网页loading加载效果,支持定时或加载完成后隐藏_阿峰博客</title><script src="https://libs.afengim.com/libs/jquery-3.1.1/jquery-3.1.1.min.js" type="application/javascript"></script><style>.loaderbg {background-color: #fff;width: 100%;height: 100%;overflow: hidden;position: fixed;left: 0;top: 0;z-index: 99999999}::-webkit-scrollbar {width: 7.5px;height: 6px;background-color: #f0f0f0;display: none}::-webkit-scrollbar-thumb {background-color: #b1b1b1;border-radius: 15px}::-webkit-scrollbar-thumb:hover {background-color: #777}.spinner {position: absolute;top: 50%;left: 50%;margin-left: -25px;margin-top: -30px;width: 50px;height: 60px;text-align: center;font-size: 10px}.spinner > .double {background: #49a9ee;height: 100%;width: 6px;display: inline-block;-webkit-animation: stretchDelay 1.2s infinite ease-in-out;animation: stretchDelay 1.2s infinite ease-in-out}.spinner .rect2 {-webkit-animation-delay: -1.1s;animation-delay: -1.1s}.spinner .rect3 {-webkit-animation-delay: -1.0s;animation-delay: -1.0s}.spinner .rect4 {-webkit-animation-delay: -0.9s;animation-delay: -0.9s}.spinner .rect5 {-webkit-animation-delay: -0.8s;animation-delay: -0.8s}@-webkit-keyframes stretchDelay {0%,40%,100% {-webkit-transform: scaleY(.4)}20% {-webkit-transform: scaleY(1)}}@keyframes stretchDelay {0%,40%,100% {transform: scaleY(.4);-webkit-transform: scaleY(.4)}20% {transform: scaleY(1);-webkit-transform: scaleY(1)}}</style> </head> <body> <!--html--> <div class="loaderbg"><div class="spinner"><div class="double rect1"></div><div class="double rect2"></div><div class="double rect3"></div><div class="double rect4"></div><div class="double rect5"></div></div> </div> <!--end--> <p><a href="https://www.afengblog.com/website-loading.html" target="_blank"><strong>使用HTML+CSS实现网页loading加载效果,支持定时或加载完成后隐藏,地址:https://www.afengblog.com/website-loading.html</strong></a> </p> <!--noscript--> <noscript><style>.loaderbg {display: none;}</style> </noscript> <!--end--> <!--script--> <script type="text/javascript">//页面加载完成之后隐藏loading/*$(window).load(function () {$(".loaderbg").hide();});*///设置页面加载3秒之后隐藏loading$(function () {setTimeout(function () {$(".loaderbg").hide();alert("页面加载完成啦!");}, 3000);}) </script> <!--end--> </body> </html>
Demo地址:使用HTML+CSS实现网页loading加载效果,支持定时或加载完成后隐藏_阿峰博客
原文地址:使用HTML+CSS实现网页loading加载效果,支持定时或加载完成后隐藏 - 阿峰博客
使用HTML+CSS实现网页loading加载效果,支持定时或加载完成后隐藏相关推荐
- html相册浏览页面怎么做,ul结合CSS制作网页相册滑动浏览效果
ul结合CSS制作网页相册滑动浏览效果 互联网 发布时间:2008-10-17 19:25:02 作者:佚名 我要评论 英文原文:Sliding Photograph Galleries ...
- [html] HTML5的video怎样预加载(支持全量加载)?
[html] HTML5的video怎样预加载(支持全量加载)? preload="auto" 个人简介 我是歌谣,欢迎和大家一起交流前后端知识.放弃很容易, 但坚持一定很酷.欢迎 ...
- html文本框如何做出立体效果,CSS教程:网页input输入框立体效果
CSS教程:网页input输入框立体效果 互联网 发布时间:2009-04-02 19:34:50 作者:佚名 我要评论 网页制作Webjx文章简介:去年常常玩开心网,耗在上面的时间也不少 ...
- html图片滚动浏览,ul结合CSS制作网页相册滑动浏览效果
别开生面纯CSS实现相册预览 ximicc.com body{ font-size:12px; color: #CC0000; } p { background-color: #F1FAFE; } # ...
- html做相册浏览,ul结合CSS制作网页相册滑动浏览效果
别开生面纯CSS实现相册预览 ximicc.com body{ font-size:12px; color: #CC0000; } p { background-color: #F1FAFE; } # ...
- html 增加等待状态,html5--等待加载效果
等待载入 你的浏览器不支持canvas var wait = document.getElementById('wait').getContext('2d'); wait.fillRect(0,0,1 ...
- 大图片加载、懒加载实现原理(滚动加载图片)
大图片加载从模糊到清晰: https://www.cnblogs.com/wangmeijian/p/6822674.html?utm_source=tuicool&utm_medium=re ...
- Html未加载完成时实现动态加载效果
在html页面未加载完成时会有一段空白,增强用户体验的话,必须在未加载完成时实现动态效果 效果如下: 1.一个普通html页面,内容如下 <html> <head> <s ...
- layui的几个简单使用(简单弹窗,加载效果,移除加载效果)
1.加载效果和移除加载效果 function layuiLoading(msg){layui.use(['layer', 'form'], function(){index = layer.load( ...
最新文章
- 如何自学python基础-零基础小白该如何学习Python?
- C# 获取 ipv4的方法
- 【解决方案】无法将grub-efi-amd64-signed软件包安装到/target/【安装Ubuntu】
- TensorFlow tf.data 导入数据(tf.data官方教程) * * * * *
- JavaFX Button和Scene点击事件代码示例
- PHP实现多服务器session共享之NFS共享
- python逻辑量有什么_Python中的逻辑运算符有什么?
- requests-session类对象-0223
- python数据分析第二讲_七月在线 Python数据分析 第二课 Numpy
- delphi xe android 黑屏,Delphi XE之路(3)解决启动时短暂的黑屏
- 汉字的 unicode 编码表
- 最好用的OCR实时翻译工具:Bob for Mac
- LeetCode答案详解
- ubuntu安装QQ教程
- java 值班管理_​运维告警的值班管理
- 给pdf加水印的方法
- 基于java springboot的图书管理系统设计和实现
- CentOS安装sox音频处理器
- 任意App/H5的web页面直接打开微信小程序的实现
- 工装,夹具,治具,检具