CSS 使用 SVG 繪制動(dòng)態(tài)皮筋與小球交互動(dòng)畫(huà)
效果展示
CSS 知識(shí)點(diǎn)
- 使用
animation
控制 SVG 的path
屬性執(zhí)行動(dòng)畫(huà) - 使用 CSS 設(shè)置 SVG 部分屬性
整體頁(yè)面布局
<div class="elasic">
<!-- 小球 -->
<div class="ball"></div>
<!-- 皮筋的陰影部分 -->
<svg>
<path></path>
</svg>
<!-- 皮筋部分 -->
<svg>
<path></path>
</svg>
</div>
繪制固定皮筋的兩個(gè)小點(diǎn)
.elasic {
position: relative;
width: 400px;
height: 400px;
display: flex;
justify-content: center;
align-items: flex-end;
}
.elasic::before {
content: "";
position: absolute;
bottom: 62.5px;
left: 5px;
width: 15px;
height: 15px;
background: #fff;
border-radius: 50%;
z-index: 2;
}
.elasic::after {
content: "";
position: absolute;
bottom: 62.5px;
right: 5px;
width: 15px;
height: 15px;
background: #fff;
border-radius: 50%;
z-index: 2;
}
實(shí)現(xiàn)上述代碼后,頁(yè)面效果如下:
繪制 SVG 皮筋
.elasic svg {
position: absolute;
width: 400px;
height: 150px;
fill: none;
}
.elasic svg:nth-child(2) {
filter: blur(25px);
}
.elasic svg path {
width: 100%;
stroke: #ff0092;
stroke-width: 10;
stroke-linecap: round;
/* 設(shè)置SVG的形狀 */
d: path("M 10 80 Q 190 80 390 80");
}
實(shí)現(xiàn) SVG 皮筋動(dòng)畫(huà)
.elasic svg path {
/* 添加動(dòng)畫(huà) */
animation: animate 2.5s linear infinite, animateColor 2.5s linear infinite;
}
@keyframes animate {
/* 這里我只是會(huì)繪制了0~60%的動(dòng)畫(huà),正在動(dòng)畫(huà)執(zhí)行時(shí)效果不是很好,可以自行補(bǔ)充剩余的動(dòng)畫(huà)步驟 */
/* 因?yàn)槲覀冎皇呛?jiǎn)單繪制了一個(gè)矩形,所以我們要呈現(xiàn)皮筋的運(yùn)動(dòng)效果的話(huà),只要修改 path 屬性中的對(duì)應(yīng)屬性就可以(簡(jiǎn)單來(lái)說(shuō)就是修改倒數(shù)第三個(gè)和第二個(gè)參數(shù)就可以形成上下運(yùn)動(dòng)的皮筋效果) */
0% {
d: path("M 10 80 Q 190 80 390 80");
}
10% {
d: path("M 10 80 Q 190 160 390 80");
}
20% {
d: path("M 10 80 Q 190 20 390 80");
}
30% {
d: path("M 10 80 Q 190 120 390 80");
}
35% {
d: path("M 10 80 Q 190 100 390 80");
}
40% {
d: path("M 10 80 Q 190 80 390 80");
}
50% {
d: path("M 10 80 Q 190 100 390 80");
}
55% {
d: path("M 10 80 Q 190 90 390 80");
}
60% {
d: path("M 10 80 Q 190 80 390 80");
}
}
/* 改變皮筋的顏色的動(dòng)畫(huà) */
@keyframes animateColor {
0%,
100% {
stroke: #ff0092;
}
33.33% {
stroke: #0f0;
}
66.66% {
stroke: #ff0;
}
}
完成上述代碼后,皮筋就可以開(kāi)始運(yùn)動(dòng),效果圖如下:
繪制小球和小球動(dòng)畫(huà)
.ball {
position: relative;
width: 60px;
height: 60px;
background-color: #fff;
border-radius: 50%;
box-shadow: inset 0 -15px 20px rgba(0, 0, 0, 0.5);
animation: animateBall 2.5s linear infinite;
}
@keyframes animateBall {
0%,
100% {
/* 控制小球的動(dòng)畫(huà)起始位置和結(jié)束位置,必須和皮筋開(kāi)始時(shí)的位置保持一致,具體可以調(diào)整參數(shù)來(lái)實(shí)現(xiàn) */
transform: translateY(-72.5px);
}
10%,
11.5% {
transform: translateY(-40px);
}
50% {
transform: translateY(-350px);
}
}
完成上述代碼后就可以完成所有效果。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-848584.html
完整代碼下載
完整代碼下載文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-848584.html
到了這里,關(guān)于CSS 使用 SVG 繪制動(dòng)態(tài)皮筋與小球交互動(dòng)畫(huà)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!