如何繪制一個(gè)圓圓的loading圈

如何繪制一個(gè)圓圓的loading圈

小程序里需要一個(gè)像下面的loading,原生的沒有,引入別的組件庫(kù)又太大,所有決定自己寫個(gè)。

1. 基本原理

分析一下,實(shí)現(xiàn)原理是首先畫出8個(gè)小圓圈,然后用css3的動(dòng)畫控制小圓圈的透明度變化。分別設(shè)置小圓圈動(dòng)畫開始時(shí)間,就可以實(shí)現(xiàn)了。 按照這樣是思路實(shí)現(xiàn)的時(shí)候發(fā)現(xiàn)效果并不理想,首先是小圓圈的位置,其次是動(dòng)畫延遲時(shí)間。俗話說(shuō)授人予魚不如授人予漁。那就逐個(gè)探討一下其中技巧。

2. 定位設(shè)置

布局的實(shí)現(xiàn)原理是父元素設(shè)置為position: relative, 每個(gè)圓圈設(shè)置樣式為position: absolute; left: xx; top: xx; right: xx; bottom: xx。 通過給left/right/top/bottom設(shè)置不同的值將其均勻的分布在一個(gè)圓圈上。
html代碼如下:

<view class="q-loading-dot-warp">  
                <view class="dot dot1"></view>
                <view class="dot dot2"></view>
                <view class="dot dot3"></view>
                <view class="dot dot4"></view>
                <view class="dot dot5"></view>
                <view class="dot dot6"></view>
                <view class="dot dot7"></view>
                <view class="dot dot8"></view>
</view>

說(shuō)起來(lái)簡(jiǎn)單,但是給它們賦值的時(shí)候沒有經(jīng)驗(yàn),第一次用理科生的思維簡(jiǎn)單將圓三等分計(jì)算坐標(biāo),往往8個(gè)圓圈就圍成了一個(gè)菱形/正方形。。。就像下面這樣


1.jpg

3. 位置設(shè)置技巧

后來(lái)在簡(jiǎn)書上看到 同學(xué)po的文章 css3實(shí)現(xiàn)18中l(wèi)oading效果, 按照J(rèn)Rd3的代碼確實(shí)可以實(shí)現(xiàn)很好看的效果,但是當(dāng)我想換一換loading圓圈大小的時(shí)候,樣式就崩了,經(jīng)過分析,他們的坐標(biāo)是存在某種數(shù)學(xué)關(guān)系的,如下圖所示,在豎直或橫線上的坐標(biāo)可通過50%定位,斜線上的坐標(biāo)如圖中所示,其中w是矩形的寬高或者說(shuō)是8個(gè)小圓圈所圍成的大園的半徑。
公式推導(dǎo)如下:

2.jpg

具體css代碼如下:

$width: 64px;
$height: 64px;
$dotWidth: 10px;
$dotHeight: 10px;
$radius: 5px;
$offset: 9.37px;

@function getLeft( $x ) {
  @return ($width/4)*$x;
}

@function getTop( $y ) {
  @return ($height/4)*$y;
}

@keyframes changeOpacity {
    from { opacity: 1; }
    to { opacity: .2; }
}

.q-loading {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    .q-loading-overlay { 
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: rgba(255, 255, 255, .5);  
    }
    .q-loading-content {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: $width;
        height: $height;
        z-index: 2;
    }
    .dot {
        width: 10px;
        height: 10px;
        position: absolute;
        background-color: #0033cc;
        border-radius: 50% 50%;
        opacity: 1;
        animation: changeOpacity 1.04s ease infinite;
    }
    .dot1 {
        left: 0;
        top: 50%;
        margin-top: -$radius;
        animation-delay: 0.13s;
    }
    .dot2 {
        left: $offset;
        top: $offset;
        animation-delay: 0.26s;
    }
    .dot3 {
        left: 50%;
        top: 0;
        margin-left: -$radius;
        animation-delay: 0.39s;
    }
    .dot4 {
        top: $offset;
        right: $offset;
        animation-delay: 0.52s;
    }
    .dot5 {
        right: 0;
        top: 50%;
        margin-top: -$radius;
        animation-delay: 0.65s;
    }
    .dot6 {
        right: $offset;
        bottom: $offset;
        animation-delay: 0.78s;
    }
    .dot7 {
        bottom: 0;
        left: 50%;
        margin-left: -$radius;
        animation-delay: 0.91s;
    }
    .dot8 {
        bottom: $offset;
        left: $offset;
        animation-delay: 1.04s;
    }
}

代碼使用scss定義了大圓和小圓圈的半徑,不管改成多大只需要更改變量,下面樣式無(wú)需改變。
通過這個(gè)公式計(jì)算的看起來(lái)就很像圓形了


3.jpg

4. 動(dòng)畫時(shí)間設(shè)置

假設(shè)動(dòng)畫持續(xù)時(shí)間為 t, 圓圈個(gè)數(shù)為 c, 某個(gè)小圓圈的位置為 i (比如上面 i 取 1~8),那么小圈相繼啟動(dòng)的時(shí)間為 i * t/c

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容