利用transition屬性實(shí)現(xiàn)一個(gè)簡(jiǎn)單小巧的hover效果

在實(shí)際工作中,簡(jiǎn)單利用css3的屬性可以讓頁(yè)面更生動(dòng),如下面的例子:

“了解詳情”的hover效果比單純的顏色變換效果更生動(dòng)。而實(shí)現(xiàn)的方式就是簡(jiǎn)單的利用了css3transition屬性。

頁(yè)面代碼結(jié)構(gòu)如下:

<div class="box">
    <a href="#">
        <span>了解詳情</span>
        <em></em>
    </a>
</div>

原理

  1. 定位一個(gè) 初始width為0的em元素,hover的時(shí)候再將 width設(shè)置為容器的width.
  2. 再利用transiton對(duì)width變化的過程設(shè)置過度效果

css如下:

.box a {        
    position: relative; 
    z-index: 0;
    display: inline-block;
    width: 158px;
    height: 38px;
    line-height: 38px;
    text-align: center;
    color: #fff;
    border: 1px solid #fff;
    border-radius: 20px;
}
.box a em {
    position: absolute;
    z-index: -1;
    top: 0px;
    left: 0px;
    display: inline-block;
    width: 0;  
    height: 100%;
    background-color: #fff;
    border-radius: 20px;
    transition: width .3s ease;
}

.box a:hover span {
    color: #00aeff;
}

.box a:hover em {
    width: 100%;  
}

參考

  1. https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions
  2. https://developer.mozilla.org/en-US/docs/Web/CSS/transition

(完)

最后編輯于
?著作權(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)容