一個簡單的CSS圓形縮放動畫

最近在作公司的登陸頁,UE同窗但願第三方登陸的圖標在hover的時候有一個圓形的縮放效果(原話是波紋效果-_-||),效果參考騰訊新聞和網易新聞的分享按鈕。html

  • 騰訊新聞的分享按鈕hover效果(新聞頁面): web

    騰訊新聞的分享按鈕hover效果

  • 網易新聞的分享按鈕hover效果(新聞頁面): sass

    網易新聞的分享按鈕hover效果

看了一下這兩個頁面的源碼,主要是用了transform:scale()transition,本身的最終的實現效果以下: bash

本身的實現效果
實現思路大致是模仿網易新聞的,佈局以下:

<a href="" class="third-party third-party-weixin">
     <i></i>
     <span></span>
 </a>
複製代碼

外層的a標籤用於總體容器和跳轉,內層的i標籤使用僞元素::before和::after分別做爲背景色和前景色,這兩個僞元素均絕對定位,垂直水平居中,::after設置縮放屬性transform:scale(0),過渡動畫屬性transition: all .3s,正常狀況下::before可見,當hover的時候::after設置縮放屬性transform:scale(1),兩個相鄰絕對定位元素在不設置z-index的狀況下,文檔流在後的元素在上,而且在有過渡動畫屬性transition的狀況下實現了縮放動畫效果。佈局

span標籤用於展現logo,能夠是圖片或者web字體,只要透明就能夠,這裏用了圖片。 CSS(此處使用的是sass)以下:字體

.third-party {
    position: relative;
    // 爲了兼容firefox必需要變成block或inline-block
    display: inline-block;
    width: 48px;
    height: 48px;
    margin: {
        left: 6%;
        right: 6%;
    }
    &:hover {
        i {
            &::after {
                transform: scale(1);
            }
        }
    }
    span {
        // position: relative是爲了兼容firefox和IE
        position: relative;
        display: block;
        width: 48px;
        height: 48px;
        background-size: 30px;
        background-position: center;
        background-repeat: no-repeat;
    }
    i {
        position: absolute;
        top: 0;
        left: 0;
        width: 48px;
        height: 48px;
        &::before {
            content: '';
            border-radius: 50%;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
        }
        &::after {
            content: '';
            transition: all .3s;
            border-radius: 50%;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            transform: scale(0);
        }
    }
    &.third-party-weixin {
        span {
            background-image: url(../images/login/weixin-64.png);
        }
        i {
            &::before {
                background-color: #20a839;
            }
            &::after {
                background-color: #30cc54;
            }
        }
    }
}
複製代碼

這樣這個簡單的圓形縮放動畫就完成啦。動畫

PS: ScreenToGif錄GIF真好用,推薦一下。url

相關文章
相關標籤/搜索