亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

如何做一個簡單的網頁加載動畫

標簽:
JavaScript

一、如何实现这个动画

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

html部分代码

<div class="wrapper">
        <div class="circle"></div>
        <div class="circle"></div></div>

css部分代码

.wrapper {    height: 100px;    width: 100px;    border: 1px solid red; /*宽高的设定,为了方便观察*/
    position: relative; /*为了将circle定位*/}.circle {    height: 10px;    width: 10px;    background-color: black;    border-radius: 100%;    /* 将circle绝对定位,当上下左右都设置为0,
    同时margin设为auto时,元素就将垂直水平居中 */
    position: absolute;    left: 0;    top: 0;    right: 0;    bottom: 0;    margin: auto;    animation: dada 2s linear infinite; /*动画名称,持续时间,线性播放,无限持续*/}.circle:nth-child(2) {    animation-delay: 1s;
}/* 从0逐渐变为半径为100的圆,同时逐渐变得透明 */@keyframes dada {
    0% {        height: 0px;        width: 0px;        opacity: 1; /*透明度1,全部显示*/
    }

    100% {        height: 100px;        width: 100px;        opacity: 0; /*透明度0,看不见了*/
    }
}

改进:如何只用一个圆实现呢?

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

 <div class="wrapper">

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代码

<div id="siteLoading" class="loading active">
        <div class="loading-animation"></div></div>

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


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消