動畫TAB切換怎樣實現

來源:互聯網
上載者:User
這次給大家帶來動畫TAB切換怎樣實現,動畫TAB切換實現的注意事項有哪些,下面就是實戰案例,一起來看一下。

設計師給了一個 tab 切換的。雖然是一個很小的功能,但是前端工程師在實現的時候還是有很多細節需要注意。我寫了一個 demo 給大家參考。

最終實現效果如下:

為了 gif 動畫能夠展示細節,我將動畫時間延長到了 3 秒

實現思路

間隔豎線,因為不是頂天立地的,所以不能用邊框。我準備用虛擬元素實現。

只有 3 個豎線,但是有 4 個 li ,這個簡單,可以用 :not(:first-child) 選取器來選擇。

切換的背景顏色變化,因為想要有從小到大的效果,因此,也不能直接使用背景顏色實現,我也準備用虛擬元素實現。

如果用虛擬元素的大小來控制,計算會比較複雜,因此,我想用 box-shadow 陰影來實現。

好,大體就是如此了,下面開始寫代碼,如下:

HTML 程式碼

  <p class="m">    <ul class="tab">      <li><a href="">導航1</a></li>      <li><a href="">導航2</a></li>      <li><a href="">導航3</a></li>      <li><a href="">導航4</a></li>    </ul>  </p>

上面的代碼結構是之前已經寫好的,我看可以,就不做任何調整了。沒有什麼累贅的代碼。

CSS 代碼

.m { margin: 100px; }.tab { width: 400px; margin: 0 auto; border: 1px solid #ddd; height: 40px; text-align: center; line-height: 40px; background: #fff; border-radius: 10px; overflow: hidden; }.tab li { float: left; width: 100px; position: relative; overflow: hidden; }.tab li:before, .tab li:after, .tab li a { -webkit-transition: all 0.25s ease-in-out; transition: all 0.25s ease-in-out; }.tab li:before, .tab li:after { content: ""; display: block; }.tab li:not(:first-child):after { background: #ddd; height: 20px; width: 1px; left: 0; top: 10px; position: absolute; }.tab li a { display: block; position: relative; z-index: 2; color: #000; font-size: 14px; }.tab li:before { width: 0; height: 0; top: 50%; left: 50%; z-index: 1; position: absolute; }.tab li:hover a { color: #fff; }.tab li:hover:before { box-shadow: 0 0 0 100px #36bc99; }.tab li:hover + li:after, .tab li:hover:after { height: 0; top: 20px; }

程式碼分析:

動畫實現非常簡單,只要使用 transition 屬性即可。

控制自己的虛擬元素和下一個同級元素的虛擬元素,只需要使用 + 選取器即可。

其他代碼都比較清晰簡單,自己分析即可。

實現這個效果還是非常簡單的,重點是平時的積累,以及各種參數的靈活搭配。想到實現方法,最終寫代碼是很快的事情。而且沒有什麼知識高點在裡面。

CSS 之所以難,不是你不會,而是不不會去搭配。

其實,還是只還原了99%的設計效果,兩條線一個在背景裡面,一個在背景外面,想要把兩條分割線都放到背景裡面來,應該如何?呢?可以思考一下。

安利一下 scss 。上面的 css 是編譯出來的。其實用 scss 實現非常方便快捷,代碼可讀性也更高。

示範如下:

.m {  margin: 100px;}.tab {  width: 400px;margin: 0 auto;border: 1px solid $cdd;height: 40px;text-align: center;line-height: 40px;  background: $cff;border-radius: 10px;overflow: hidden;  li {    float: left;width: 100px;position: relative;overflow: hidden;    &:before,&:after,a {@include dz();}    &:before,&:after {      content: "";display: block;    }    &:not(:first-child) {      &:after {        background: $cdd;height: 20px;width: 1px;left: 0;top: 10px;position: absolute;      }    }    a {      display: block;position: relative;z-index: 2;color: $c00;font-size: 14px;    }    &:before {      width: 0;height: 0;top: 50%;left: 50%;z-index: 1;position: absolute;    }    &:hover {      a {color: $cff;}      &:before {        box-shadow: 0 0 0 100px $cyan;      }      & + li:after,&:after {        height: 0;top: 20px;      }    }  }}

當然,這段代碼中我用了顏色變數以及 mixin 混入代碼。你不能直接使用。

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

css3的pointer-events使用詳解

focus-within的使用詳解

相關文章

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.