HTML5+CSS3做一組邊框滑動(dòng)按鈕的懸停效果苹享,代碼不多且相對(duì)簡(jiǎn)單公罕,帶大家鞏固一下CSS自定義屬性的使用身堡。
效果:
源碼:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>邊框滑動(dòng)按鈕的懸停效果</title>
<link rel="stylesheet" href="../css/30.css">
</head>
<body>
<div class="container">
<button class="btn">點(diǎn)贊</button>
<button class="btn">關(guān)注</button>
<button class="btn">評(píng)論</button>
<button class="btn">轉(zhuǎn)發(fā)</button>
</div>
</body>
</html>
*{
/* 初始化 取消頁(yè)面元素的內(nèi)外邊距 */
margin: 0;
padding: 0;
}
body{
/* 自定義屬性 背景顏色 可通過(guò)var函數(shù)調(diào)用 */
--bgc: #353b48;
background-color: var(--bgc);
/* 100%窗口高度 */
height: 100vh;
/* 彈性布局 水平们童、垂直居中 */
display: flex;
justify-content: center;
align-items: center;
}
.container{
/* 彈性布局 */
display: flex;
/* 水平排列 */
flex-direction: row;
/* 允許換行 */
flex-wrap: wrap;
justify-content: space-around;
width: 500px;
}
.btn{
width: 200px;
height: 60px;
background: none;
border: 4px solid;
color: var(--c);
cursor: pointer;
font-size: 16px;
font-weight: 700;
margin: 20px;
/* 相對(duì)定位 */
position: relative;
}
.btn::before,.btn::after{
content: "";
/* 絕對(duì)定位 */
position: absolute;
width: 14px;
height: 4px;
/* 與背景同色 */
background-color: var(--bgc);
/* 沿X軸傾斜30度 */
transform: skewX(30deg);
/* 動(dòng)畫過(guò)渡 */
transition: 0.4s linear;
}
.btn::before{
top: -4px;
left: 10%;
}
.btn::after{
bottom: -4px;
right: 10%;
}
.btn:hover::before{
left: 80%;
}
.btn:hover::after{
right: 80%;
}
/* 設(shè)置每一個(gè)按鈕的自定義顏色屬性--c */
.btn:nth-child(1){
--c: #4ad3e2;
}
.btn:nth-child(2){
--c: #93EDD4;
}
.btn:nth-child(3){
--c: #F9CB8F;
}
.btn:nth-child(4){
--c: #ffb1a3;
}