CSS偽類:CSS3滑鼠滑過按鈕動畫第三節

  • 2019 年 12 月 30 日
  • 筆記

前言

有了之前的兩章,小夥伴們對按鈕懸浮動畫是否又有了新的認識呢?

前面兩章主要是從背景著手,而本章主要是圍繞邊框動畫做效果。並且,本章節(按鈕組:有趣的CSS按鈕動畫,帶你進入CSS世界)也就到此結束了,本章結尾會對前3小節進行一定的總結。

下面繼續本小節的主題,請先看一下效果示例:

看過前兩小節的小夥伴,可能不需要看下面的源碼,就知道如何實現了,大家可以先自己動手試試,然後再回頭來看看。或許實現方式不一樣,但只要能實現,都是好方法。

下面對示例講解

示例一

<button class="btn-1">按鈕一</button>    <style>  button{    position: relative;    width: 100px;    height: 40px;    background: none;    cursor: pointer;    color: #fff;    border: none;    margin-right: 20px;    margin-bottom: 20px;  }  button:before,  button:after{    position: absolute;    content: '';    width: 100%;    height: 100%;    z-index: 10;    transition: all .5s;  }  /* 按鈕一 */  .btn-1:before, .btn-1:after{    height: 2px;    left: 50%;    width: 0;    background: #f13f84;    transform: translateX(-50%);  }  .btn-1:before{    top: 0;  }  .btn-1:after{    bottom: 0;  }  .btn-1:hover:before,  .btn-1:hover:after{    width: 100%;  }  </style>

解析:

1、:before top為0,:after bottom為0,高度height: 2px,而寬度為0,並且水平居中

2、在絕對定位的作用下,:hover改變:before:after的寬度,即可形成上圖效果

示例二

<button class="btn-2">按鈕二</button>    <style>  ...  /* 這裡省略上方的公共樣式 */  .btn-2{    background: #333;    height: 36px;  }  .btn-2:before,  .btn-2:after{    width: 10px;    height: 10px;    background: #f13f84;    mix-blend-mode: hue;  }  .btn-2:before {    left: -2px;    top: -2px;  }  .btn-2:after {    right: -2px;    bottom: -2px;  }  .btn-2:hover:before,  .btn-2:hover:after{    height: calc(100% + 4px);    width: calc(100% + 4px);  }  </style>

解析:

1、:before:after,超出button 2px

2、:hover時改變:before:after寬高,這裡寬高用到了calc()

calc() 函數用於動態計算長度值。

● 需要注意的是,運算符前後都需要保留一個空格,例如:width: calc(100% - 10px)

● 任何長度值都可以使用calc()函數進行計算;

calc()函數支援 "+", "-", "*", "/" 運算;

calc()函數使用標準的數學運算優先順序規則;

3、大家應該都留意到了上圖中,特意操作了一個屬性mix-blend-mode,它在這裡的作用讓button的背景顯示出來覆蓋在:before:after背景色的上方。

css3 mix-blend-mode語法

mix-blend-mode:normal | multiply | screen | overlay | darken | lighten | color-dodge | color-burn | hard-light | soft-light | difference | exclusion | hue | saturation | color | luminosity

hue 色相模式 「色相」模式只用「混合色」顏色的色相值進行著色,而使飽和度和亮度值保持不變。

這裡就不具體講述mix-blend-mode,希望後面能用一章來專門講解。

示例三

<button class="btn-3">    <span>按鈕三</span>  </button>    <style>  ...  /* 這裡省略上方的公共樣式 */  button span:before,  button span:after{    position: absolute;    content: '';    width: 100%;    height: 100%;    z-index: 10;    transition: all .5s;  }  .btn-3{    background: #333;    height: 36px;  }  .btn-3:before,  .btn-3:after,  .btn-3 span:before,  .btn-3 span:after{    width: 10px;    height: 10px;    background: #f13f84;    mix-blend-mode: hue;  }  .btn-3:before {    left: -2px;    top: -2px;  }  .btn-3:after {    right: -2px;    top: -2px;  }  .btn-3 span:before {    left: -2px;    bottom: -2px;  }  .btn-3 span:after {    right: -2px;    bottom: -2px;  }  .btn-3:hover:before,  .btn-3:hover:after,  .btn-3:hover span:before,  .btn-3:hover span:after {    height: 60%;    width: 60%;  }

解析:

1、示例三就是示例二的升級版,用span的偽類來完善按鈕的四隻角

2、:hover時改變四個偽類的寬高即可。

示例四

<button class="btn-4">按鈕四</button>    <style>  ...  /* 這裡省略上方的公共樣式 */  .btn-4{    height: 34px;    border: 1px solid #f13f84;  }  .btn-4:before,  .btn-4:after{    width: 10px;    height: 10px;    border-style: solid;    border-color: #f13f84;  }  .btn-4:before {    left: -4px;    top: -4px;    border-width: 1px 0 0 1px;  }  .btn-4:after {    right: -4px;    bottom: -4px;    border-width: 0 1px 1px 0;  }  .btn-4:hover:before,  .btn-4:hover:after{    height: calc(100% + 7px);    width: calc(100% + 7px);  }

解析:

1、示例四是示例二的另外一種實現方式,不過區別是按鈕加了一個邊框

2、:before:after直接設置border,而不是用background來展示對角樣式。

width: 10px;  height: 10px;  border-style: solid;  border-color: #f13f84;  border-width: 1px 0 0 1px;

3、然後:hover時改變偽類寬高,即可

示例五

<button class="btn-5">按鈕五</button>    <style>  ...  /* 這裡省略上方的公共樣式 */  .btn-5{    background: #333;    height: 34px;    border: 1px solid #fff;  }  .btn-5:before,  .btn-5:after{    width: 10px;    height: 10px;    border-style: solid;    border-color: #fff;  }  .btn-5:before {    left: -4px;    top: -4px;    border-width: 1px 0 0 1px;  }  .btn-5:after {    right: -4px;    bottom: -4px;    border-width: 0 1px 1px 0;  }  .btn-5:hover{    border-color: #f13f84;  }  .btn-5:hover:before,  .btn-5:hover:after{    height: calc(100% + 7px);    width: calc(100% + 7px);    border-color: #f13f84;    transform: rotateY(180deg);  }

解析:

1、示例五,與示例四隻有2點區別,:hover時,使其偽類旋轉180°,同時改變邊框顏色

border-color: #f13f84;  transform: rotateY(180deg);

示例六

<button class="btn-6">    <span>按鈕六</span>  </button>    <style>  ...  /* 這裡省略上方的公共樣式 */  .btn-6{    overflow: hidden;  }  .btn-6:before,  .btn-6:after,  .btn-6 span:before,  .btn-6 span:after{    background: linear-gradient(to right, rgba(0,0,0,0), #f13f84);    transition: all 2s;  }  .btn-6:before,  .btn-6:after{    width: 100%;    height: 1px;  }  .btn-6:before {    top: 0;    left: -100%;  }  .btn-6:after {    bottom: 0;    right: -100%;  }  .btn-6 span:before, .btn-6 span:after{    width: 1px;    height: 100%;  }  .btn-6 span:before {    bottom: -100%;    left: 0;  }  .btn-6 span:after {    top: -100%;    right: 0;  }  .btn-6:hover:before{    animation: topA 1s linear infinite;    animation-delay: .5s;  }  @keyframes topA{    100%{      left: 100%;    }  }  .btn-6:hover span:after{    animation: rightA 1s linear infinite;    animation-delay: 1s;  }  @keyframes rightA{    100%{      top: 100%;    }  }  .btn-6:hover:after{    animation: bottomA 1s linear infinite;    animation-delay: 1.5s;  }  @keyframes bottomA{    100%{      right: 100%;    }  }  .btn-6:hover span:before {    animation: leftA 1s linear infinite;    animation-delay: 2s;  }  @keyframes leftA{    100%{      bottom: 100%;    }  }

解析:

1、示例六,可以說和示例三有一點點相似之處吧,升級版

2、也是通過四個偽類,分別分布在按鈕的上右下左位置,上下的偽類高度是1px,寬是100%,左右的偽類寬度是1px,高是100%,同時設置背景為線性漸變linear-gradient

3、:hover時,上方偽類從左邊-100%的位置,向左邊100%的位置運動;右邊偽類從上方-100%的位置,向上方100%的位置運動;下發偽類從右邊-100%的位置,向右邊100%的位置運動;左邊偽類從下方-100%的位置,向下方100%的位置運動。然後設置延時執行動畫,即可。

需要注意的是延時執行動畫(animation-delay)時間,一定要調整好,否則看起來就沒有流暢,銜接會出現問題。

示例七

<button class="btn-7">    <svg height="100%" width="100%" xmlns="http://www.w3.org/2000/svg">      <rect class="outline" height="100%" width="100%" />      <div class="text">按鈕七</div>    </svg>  </button>    <style>  ...  /* 這裡省略上方的公共樣式 */  .btn-7{    position: relative;    color: #f13f84;    text-decoration: none;    width: 250px;    height: 50px;    margin: 50px auto;    overflow: hidden;  }  .btn-7 .outline {    position: absolute;    top: 0;    left: 0;    width: 100%;    height: 100%;    stroke: #f13f84;    stroke-width: 2px;    fill: transparent;    stroke-dasharray: 100 500;    stroke-dashoffset: 225;    transition: all .5s;    box-sizing: border-box;  }  .btn-7 .text {    position: relative;    top: -35px;    line-height: 1;    letter-spacing: 1px;    text-transform: uppercase;  }  .btn-7:hover .outline{    stroke-dasharray: 600 0;    stroke-dashoffset: 475;  }

解析:

1、示例七,是一種全選的方式,svg

2、svg 元素描述 – <text> 元素用於定義文本

  • <rect> 定義為矩形形狀(圓形 <circle>、橢圓 <ellipse>、線 <line>、折線 <polyline>、多邊形 <polygon>、路徑 <path>

3、svg 屬性描述

  • stroke 定義一條線,文本或元素輪廓顏色
  • stroke-width 屬性定義了一條線,文本或元素輪廓厚度
  • stroke-dasharray 屬性用來設置描邊的點劃線的圖案範式。就是設置實線和虛線的寬度。即有或者沒有線段的長度。
  • stroke-dashoffset 則指定了dash模式到路徑開始的距離

具體,後面也提供專門章節講述

總結

本章節(按鈕組:有趣的CSS按鈕動畫,帶你進入CSS世界)到此就結束了,首先謝謝大家的支援。

通過本章節,小夥伴們都學到了什麼?

1、思想,每個小節,示例都是從易至難,循序漸進;

2、CSS 偽類元素:before:after的運用

3、html元素的布局,元素水平垂直居中

4、transitionanimation動畫,它們有什麼區別呢(想知道答案的可以看小編歷史文章)?

5、CSS3 線性漸變和徑向漸變

6、相對定位和絕對定位靈活運用

7、transform 元素移動、變形、旋轉等

居然有這麼多知識點,你都學到了嗎?

小夥伴們,有問題可以評論區留言哦,歡迎大家點評。