|
|
|
|
|
本文介紹一個(gè)CSS按鈕,它具有伸縮(鼠標(biāo)懸停)效果,即是當(dāng)鼠標(biāo)放上時(shí),按鈕伸長,當(dāng)鼠標(biāo)移開后,按鈕縮短。這個(gè)效果在很多網(wǎng)頁上都挺常見的,它多用于“更多”的按鈕設(shè)計(jì)上。
實(shí)例簡介
默認(rèn)狀態(tài)按鈕是實(shí)心圓形,內(nèi)部中間有一個(gè)向右的小箭頭。
當(dāng)鼠標(biāo)移到按鈕上時(shí),按鈕向右伸長,變成圓角矩形。
當(dāng)鼠標(biāo)移開按鈕時(shí),按鈕又縮回到默認(rèn)狀態(tài)。
它多用于“更多”的按鈕設(shè)計(jì)上。
完整HTML代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Css Button Hover</title>
<style>
* {
box-sizing: border-box;
}
*::before, *::after {
box-sizing: border-box;
}
body {
font-family: "Mukta", sans-serif;
font-size: 1rem;
line-height: 1.5;
display: flex;
align-items: center;
justify-content: center;
margin: 0;
min-height: 100vh;
background: #f3f8fa;
}
button {
position: relative;
display: inline-block;
cursor: pointer;
outline: none;
border: 0;
vertical-align: middle;
text-decoration: none;
background: transparent;
padding: 0;
font-size: inherit;
font-family: inherit;
}
button.learn-more {
width: 12rem;
height: auto;
}
button.learn-more .circle {
transition: all 0.45s cubic-bezier(0.65, 0, 0.076, 1);
position: relative;
display: block;
margin: 0;
width: 3rem;
height: 3rem;
background: #282936;
border-radius: 1.625rem;
}
button.learn-more .circle .icon {
transition: all 0.45s cubic-bezier(0.65, 0, 0.076, 1);
position: absolute;
top: 0;
bottom: 0;
margin: auto;
background: #fff;
}
button.learn-more .circle .icon.arrow {
transition: all 0.45s cubic-bezier(0.65, 0, 0.076, 1);
left: 0.625rem;
width: 1.125rem;
height: 0.125rem;
background: none;
}
button.learn-more .circle .icon.arrow::before {
position: absolute;
content: "";
top: -0.25rem;
right: 0.0625rem;
width: 0.625rem;
height: 0.625rem;
border-top: 0.125rem solid #fff;
border-right: 0.125rem solid #fff;
transform: rotate(45deg);
}
button.learn-more .button-text {
transition: all 0.45s cubic-bezier(0.65, 0, 0.076, 1);
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
padding: 0.75rem 0;
margin: 0 0 0 1.85rem;
color: #282936;
font-weight: 700;
line-height: 1.6;
text-align: center;
text-transform: uppercase;
}
button:hover .circle {
width: 100%;
}
button:hover .circle .icon.arrow {
background: #fff;
transform: translate(1rem, 0);
}
button:hover .button-text {
color: #fff;
}
@supports (display: grid) {
body {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 0.625rem;
grid-template-areas: ". main main ." ". main main .";
}
#container {
grid-area: main;
align-self: center;
justify-self: center;
}
}
</style>
</head>
<body>
<div id="container">
<button class="learn-more">
<span class="circle" aria-hidden="true">
<span class="icon arrow"></span>
</span>
<span class="button-text">Learn More</span>
</button>
</div>
</body>
</html>
代碼解釋
1、動(dòng)畫速度可以通過CSS代碼改變
transition: all 0.45s cubic-bezier(0.65, 0, 0.076, 1);
0.45s 是定義動(dòng)畫的速度,數(shù)值越小速度越快。
2、按鈕顏色、大小、圓角等樣式設(shè)計(jì)可通過下面的CSS代碼定義
button.learn-more .circle {
transition: all 0.45s cubic-bezier(0.65, 0, 0.076, 1);
position: relative;
display: block;
margin: 0;
width: 3rem;
height: 3rem;
background: #282936;
border-radius: 1.625rem;
}
border-radius
是定義按鈕圓角大小。background
是定義按鈕顏色。