CSS實現動態氣泡背景代碼分享

來源:互聯網
上載者:User
本文主要和大家介紹了CSS 動畫實現動態氣泡背景的方法的相關資料,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能協助到大家。

今天的第一個任務是寫個登入頁面,老大給了我一個參(chao)考(xi)案例,大家點選連結就能看到。嗯,這個登入頁面確實很簡潔、大方,尤其是它的氣泡背景,第一反應這應該是張動態圖片,開啟審查元素才發現原來這是用代碼寫的,一下子激起了寶寶的好奇心,所以也試著寫了一個帶氣泡背景的登入頁面,效果如下:

emm...為什麼上傳的 gif 動態圖總是這麼小,來補張:

(大家可以自行腦補這些背景氣泡往上升的畫面 :sob:)

只需一些簡單的代碼就可以實現這樣的效果,

首先我們先定義10個 li 列表標籤,我用的是 vue 架構:


<ul class="bg-bubbles">    <li v-for="(item, index) in bubbles" :key="index"></li></ul>


created() {    this.bubbles.length = 10; },

樣式是用 less 編寫的:


.bg-bubbles {    position: absolute;    // 使氣泡背景充滿整個螢幕    top: 0;    left: 0;    width: 100%;    height: 100%;    li {      position: absolute;      // bottom 的設定是為了營造出氣泡從頁面底部冒出的效果;      bottom: -160px;      // 預設的泡泡大小;      width: 40px;      height: 40px;      background-color: rgba(255, 255, 255, 0.15);      list-style: none;      // 使用自訂動畫使氣泡漸現、上升和翻滾;      animation: square 15s infinite;      transition-timing-function: linear;      // 分別設定每個氣泡不同的位置、大小、透明度和速度,以顯得有層次感;      &:nth-child(1) {        left: 10%;      }      &:nth-child(2) {        left: 20%;        width: 90px;        height: 90px;        animation-delay: 2s;        animation-duration: 7s;      }      &:nth-child(3) {        left: 25%;        animation-delay: 4s;      }      &:nth-child(4) {        left: 40%;        width: 60px;        height: 60px;        animation-duration: 8s;        background-color: rgba(255, 255, 255, 0.3);      }      &:nth-child(5) {        left: 70%;      }      &:nth-child(6) {        left: 80%;        width: 120px;        height: 120px;        animation-delay: 3s;        background-color: rgba(255, 255, 255, 0.2);      }      &:nth-child(7) {        left: 32%;        width: 160px;        height: 160px;        animation-delay: 2s;      }      &:nth-child(8) {        left: 55%;        width: 20px;        height: 20px;        animation-delay: 4s;        animation-duration: 15s;      }      &:nth-child(9) {        left: 25%;        width: 10px;        height: 10px;        animation-delay: 2s;        animation-duration: 12s;        background-color: rgba(255, 255, 255, 0.3);      }      &:nth-child(10) {        left: 85%;        width: 160px;        height: 160px;        animation-delay: 5s;      }    }    // 自訂 square 動畫;    @keyframes square {      0% {        opacity: 0.5;        transform: translateY(0px) rotate(45deg);      }      25% {        opacity: 0.75;        transform: translateY(-400px) rotate(90deg)      }      50% {        opacity: 1;        transform: translateY(-600px) rotate(135deg);      }      100% {        opacity: 0;        transform: translateY(-1000px) rotate(180deg);      }    }  }

至此,一個氣泡背景圖就完成了。回過頭來看,確實不難,但也愈來愈讓人感受到 css 動畫的魅力和強大 :relaxed:。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.