自己寫一個純 css 有意思的邊框真的很難,這篇文章的邊框,其實我是不滿意的,但通過它來學習 css ,還是有一定作用的。
效果圖:
特點:支持圓角,
問題:
調整寬窄的時候,都要調整運動函數,比較麻煩。
說真的,不好看,也不酷
html
<div className={styles.father_box_two}> <div className={styles.box_two}>內容內容</div> </div>
css
::after{} 和 ::before{} 兩個偽類,分別實現了一種環繞效果,這里是將他倆結合在一起之后的效果,任何一個單獨使用都是可以的。
::after{} 實現的是矩形圍繞,但是沒有漸變的感覺
::before{} 實現了漸變的效果,但是會有一個切角,不美觀
.father_box_two{ width: 100px; height: 100px; line-height: 100px; // border-radius: 10%; overflow: hidden; position: relative; background-color: rgba(0,0,0,.06); .box_two{ position: absolute; top: 0; left: 0; width: 100%; height: 100%; transform-origin: center center; // 一中心未基點 transform: scale(0.95); // 縮小到原來的 95% // border-radius: 10%; background-color: #ffffff; z-index: 1; } &::after{ content: ""; position: absolute; display: inline-block; background-color: #1a73e8; top: 0; left: 0; width: 100px; height: 5px; animation: bgmove 5s linear infinite; @keyframes bgmove { 0% { top: 0; left: 0; width: 100px; height: 5px; } 25% { top: 0; left: 0; width: 5px; height: 100px; } 50% { top: 95px; left: 0; width: 100px; height: 5px; } 75% { top: 0px; left: 95px; width: 5px; height: 100px; } 100% { top: 0; left: 0; width: 100px; height: 5px; } } } &::before{ content: ""; width: 100%; height: 100%; position: absolute; top: 0px; left: 0px; transform-origin: center center; transform: rotate(30deg) scale(2); background: conic-gradient( #1a73e8, rgba(0,0,0,.06), rgba(0,0,0,.06), rgba(0,0,0,.06) ); animation: rotate 5s linear infinite; @keyframes rotate { 100% { transform: rotate(-330deg) scale(2); } } } }
還可以利用 clip 屬性,剪裁的方式進行,繪制相同效果的環繞邊框,此方式無法實現圓角
.border { width: 200px; height: 200px; margin: auto; box-shadow: inset 0 0 0 2px #999; position: relative; overflow: hidden; div { width: 10px; height: 10px; position: absolute; } .content{ position: absolute; width: 196px; height: 196px; top: 2px; left: 2px; background-color: #fff; } &::after { content: ''; position: absolute; top: 0; left: 0; margin: 0%; display: inline-block; width: 100%; height: 100%; border: 2px solid #1a73e8; /* 等同於 box-shadow: inset 0 0 0 2px #1a73e8; 或者 outline: 3px solid #1a73e8; outline-offset: -3px; */ animation: clipMe 5s linear infinite; @keyframes clipMe { 0%, 100% { clip: rect(0px, 200px, 2px, 0px); } 25% { clip: rect(0px, 2px, 200px, 0px); } 50% { clip: rect(198px, 200px, 200px, 0px); } 75% { clip: rect(0px, 200px, 200px, 198px); } } } &::before{ content: ""; width: 100%; height: 100%; position: absolute; top: 0px; left: 0px; transform-origin: center center; transform: rotate(30deg) scale(2); background: conic-gradient( #1a73e8, rgba(0,0,0,.06), rgba(0,0,0,.06), rgba(0,0,0,.06) ); animation: rotate 5s linear infinite; @keyframes rotate { 100% { transform: rotate(-330deg) scale(2); } } } }