這里其實是借用了元素的兩個偽元素。兩個偽元素分別只設置上、左邊框,下、右邊框,通過 hover 時改變兩個偽元素的高寬即可。非常好理解。
div {
? ? position: relative;
? ? border: 1px solid #03A9F3;
? ? &::before,
? ? &::after {
? ? ? ? content: "";
? ? ? ? position: absolute;
? ? ? ? width: 20px;
? ? ? ? height: 20px;
? ? }
? ? &::before {
? ? ? ? top: -5px;
? ? ? ? left: -5px;
? ? ? ? border-top: 1px solid var(--borderColor);
? ? ? ? border-left: 1px solid var(--borderColor);
? ? }
? ? &::after {
? ? ? ? right: -5px;
? ? ? ? bottom: -5px;
? ? ? ? border-bottom: 1px solid var(--borderColor);
? ? ? ? border-right: 1px solid var(--borderColor);
? ? }
? ? &:hover::before,
? ? &:hover::after {
? ? ? ? width: calc(100% + 9px);
? ? ? ? height: calc(100% + 9px);
? ? }
}
CodePen Demo -- width border animation
接下來,會開始加深一些難度。
虛線邊框動畫
使用 dashed 關鍵字,可以方便的創(chuàng)建虛線邊框。
1
2
3
div {
? ? border: 1px dashed #333;
}
當然,我們的目的是讓邊框能夠動起來。使用 dashed 關鍵字是沒有辦法的。但是實現(xiàn)虛線的方式在 CSS 中有很多種,譬如漸變就是一種很好的方式:
1
2
3
4
5
div {
? ? background: linear-gradient(90deg, #333 50%, transparent 0) repeat-x;
? ? background-size: 4px 1px;
? ? background-position: 0 0;
}
看看,使用漸變模擬的虛線如下:
好,漸變支持多重漸變,我們把容器的 4 個邊都用漸變表示即可:
div {
? ? background:
? ? ? ? linear-gradient(90deg, #333 50%, transparent 0) repeat-x,
? ? ? ? linear-gradient(90deg, #333 50%, transparent 0) repeat-x,
? ? ? ? linear-gradient(0deg, #333 50%, transparent 0) repeat-y,
? ? ? ? linear-gradient(0deg, #333 50%, transparent 0) repeat-y;
? ? background-size: 4px 1px, 4px 1px, 1px 4px, 1px 4px;
? ? background-position: 0 0, 0 100%, 0 0, 100% 0;
}
效果如下:
OK,至此,我們的虛線邊框動畫其實算是完成了一大半了。雖然 border-style: dashed 不支持動畫,但是漸變支持呀。我們給上述 div 再加上一個 hover 效果,hover 的時候新增一個 animation 動畫,改變元素的 bakcground-position 即可。
div:hover {
? ? animation: linearGradientMove .3s infinite linear;
}
@keyframes linearGradientMove {
? ? 100% {
? ? ? ? background-position: 4px 0, -4px 100%, 0 -4px, 100% 4px;
? ? }
}
OK,看看效果,hover 上去的時候,邊框就能動起來,因為整個動畫是首尾相連的,無限循環(huán)的動畫看起來就像是虛線邊框在一直運動,這算是一個小小的障眼法或者小技巧:
這里還有另外一個小技巧,如果我們希望虛線邊框動畫是從其他邊框,過渡到虛線邊框,再行進動畫。完全由漸變來模擬也是可以的,如果想節(jié)省一些代碼,使用 border 會更快一些,譬如這樣:
div {
? ? border: 1px solid #333;
? ? &:hover {
? ? ? ? border: none;
? ? ? ? background:
? ? ? ? ? ? linear-gradient(90deg, #333 50%, transparent 0) repeat-x,
? ? ? ? ? ? linear-gradient(90deg, #333 50%, transparent 0) repeat-x,
? ? ? ? ? ? linear-gradient(0deg, #333 50%, transparent 0) repeat-y,
? ? ? ? ? ? linear-gradient(0deg, #333 50%, transparent 0) repeat-y;
? ? ? ? background-size: 4px 1px, 4px 1px, 1px 4px, 1px 4px;
? ? ? ? background-position: 0 0, 0 100%, 0 0, 100% 0;
? ? }
}
由于 border 和 background 在盒子模型上位置的差異,視覺上會有一個很明顯的錯位的感覺:
要想解決這個問題,我們可以把 border 替換成 outline,因為 outline 可以設置 outline-offset。便能完美解決這個問題:
8
div {
? ? outline: 1px solid #333;
? ? outline-offset: -1px;
? ? &:hover {
? ? ? ? outline: none;
? ? }
}
最后看看運用到實際按鈕上的效果:
上述 Demo 完整代碼如下:
CodePen Demo -- dashed border animation
其實由于背景和邊框的特殊關系,使用 border 的時候,通過修改 background-position 也是可以解決的,就是比較繞。關于背景和邊框的填充關系,可以看這篇文章:條紋邊框的多種實現(xiàn)方式
漸變的其他妙用
利用漸變,不僅僅只是能完成上述的效果。
我們繼續(xù)深挖漸變,利用漸變實現(xiàn)這樣一個背景:
16
div {
? ? position: relative;
? ? &::after {
? ? ? ? content: '';
? ? ? ? position: absolute;
? ? ? ? left: -50%;
? ? ? ? top: -50%;
? ? ? ? width: 200%;
? ? ? ? height: 200%;
? ? ? ? background-repeat: no-repeat;
? ? ? ? background-size: 50% 50%, 50% 50%;
? ? ? ? background-position: 0 0, 100% 0, 100% 100%, 0 100%;
? ? ? ? background-image: linear-gradient(#399953, #399953), linear-gradient(#fbb300, #fbb300), linear-gradient(#d53e33, #d53e33), linear-gradient(#377af5, #377af5);
? ? }
}
注意,這里運用了元素的偽元素生成的這個圖形,并且,寬高都是父元素的 200%,超出則 overflow: hidden。
接下來,給它加上旋轉:
div {
? ? animation: rotate 4s linear infinite;
}
@keyframes rotate {
? ? 100% {
? ? ? ? transform: rotate(1turn);
? ? }
}
看看效果:
最后,再利用一個偽元素,將中間遮罩起來,一個 Nice 的邊框動畫就出來了:
上述 Demo 完整代碼如下,這個效果我最早見于這位作者 -- Jesse B
CodePen Demo -- gradient border animation
改變漸變的顏色
掌握了上述的基本技巧之后,我們可以再對漸變的顏色做一些調整,我們將 4 種顏色變成 1 種顏色:
div::after {
? ? content: '';
? ? position: absolute;
? ? left: -50%;
? ? top: -50%;
? ? width: 200%;
? ? height: 200%;
? ? background-color: #fff;
? ? background-repeat: no-repeat;
? ? background-size: 50% 50%;
? ? background-position: 0 0;
? ? background-image: linear-gradient(#399953, #399953);
}
得到這樣一個圖形:
同樣的,讓它旋轉一起,一個單色追逐的邊框動畫就出來了:
CodePen Demo -- gradient border animation 2
Wow,很不錯的樣子。不過如果是單線條,有個很明顯的缺陷,就是邊框的末尾是一個小三角而不是垂直的,可能有些場景不適用或者 PM 接受不了。
那有沒有什么辦法能夠消除掉這些小三角呢?有的,在下文中我們再介紹一種方法,利用 clip-path ,消除掉這些小三角。
conic-gradient 的妙用
再介紹 clip-path 之前,先講講角向漸變。
上述主要用到了的是線性漸變 linear-gradient 。我們使用角向漸變 conic-gradient 其實完全也可以實現(xiàn)一模一樣的效果。
我們試著使用 conic-gradient 也實現(xiàn)一次,這次換一種暗黑風格。核心代碼如下:
.conic {
? ? position: relative;
? ? &::before {
? ? ? ? content: '';
? ? ? ? position: absolute;
? ? ? ? left: -50%;
? ? ? ? top: -50%;
? ? ? ? width: 200%;
? ? ? ? height: 200%;
? ? ? ? background: conic-gradient(transparent, rgba(168, 239, 255, 1), transparent 30%);
? ? ? ? animation: rotate 4s linear infinite;
? ? }
}
@keyframes rotate {
? ? 100% {
? ? ? ? transform: rotate(1turn);
? ? }
}
深圳網站建設www.sz886.com