栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

如何做一个简单的网页加载动画

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何做一个简单的网页加载动画

一、如何实现这个动画思路:一个黑色实心圆逐渐变大,同时透明度逐渐降低。然后将第二个相同圆的动画效果延时1s。

html部分代码

        
        

css部分代码

.wrapper {    height: 100px;    width: 100px;    border: 1px solid red; 
    position: relative; }.circle {    height: 10px;    width: 10px;    background-color: black;    border-radius: 100%;    
    position: absolute;    left: 0;    top: 0;    right: 0;    bottom: 0;    margin: auto;    animation: dada 2s linear infinite; }.circle:nth-child(2) {    animation-delay: 1s;
}@keyframes dada {
    0% {        height: 0px;        width: 0px;        opacity: 1; 
    }

    100% {        height: 100px;        width: 100px;        opacity: 0; 
    }
}
改进:如何只用一个圆实现呢?

用伪元素::before和::after。
html部分代码:只需要用一个容器,容器本身用来定位

 

css部分代码:容器中两个圆,用::befor和::after来实现

.wrapper {  height: 200px;  width: 200px;  border: 1px solid red;  
  position: relative;
}.wrapper::before,.wrapper::after{  content: '';  height: 10px;  width: 10px;  background-color: black;  border-radius: 100%;  
  position: absolute;  left: 0;  top: 0;  bottom: 0;  right: 0;  margin: auto;  animation: dada 2s linear infinite;
}.wrapper::after {  animation-delay: 1s;
}

@keyframes dada {
  0% {    height: 0px;    width: 0px;    opacity: 1;
  }
  100% {    height: 100px;    width: 100px;    opacity: 0;
  }
}
二、将动画效果加入到网页中思路:1、采用fixed,让其置于所有页面的正上方。2、然后为其添加一个状态active,当页面加载完毕时,去除active,使其不可见。

html代码

        

css部分代码

.loading {  display: none;  background-color: antiquewhite;  position: fixed;  top: 0;  left: 0;  height: 100%;  width: 100%;  z-indx: 1;  justify-content: center;  align-items: center;
}.loading.active {  display: flex;
}

js部分代码:当页面加载完毕时(在body下添加script即可),去除掉loading中的active的class名

  setTimeout(function(){
      siteLoading.classList.remove('active')
    },2000)

这里的setTimeout设置是为了2000ms的延迟触发,不然网速太快,loading动画根本看不见啦。。。



作者:饥人谷_朱笑笑啊
链接:https://www.jianshu.com/p/5f88ef3d980d


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/245828.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号