直接上代码,CSS:
.icon-play {
width: 100px;
height: 100px;
fill: #089;
cursor: pointer
}
.icon-play path {
d: path('M 12,26 18.5,22 18.5,14 12,10 z M 18.5,22 25,18 25,18 18.5,14 z');
transition: d .2s linear .1s
}
.icon-play.play path {
d: path('M 12,26 16,26 16,10 12,10 z M 21,26 25,26 25,10 21,10 z')
}
HTML:
<!-- 没错只需要这一行! -->
<svg class="icon-play" viewBox="0 0 36 36"><path/></svg>
或者想加个背景圆
<svg class="icon-play" viewBox="0 0 36 36">
<circle cx="18" cy="18" r="18" fill="#000"/>
<path/>
</svg>
这样只需要通过JS控制
.icon-play
标签添加或移除
.play
就可以啦
// js 添加移除 .play
var iconPlay = document.querySelector('.icon-play')
iconPlay.addEventListener('click', function () {
if (iconPlay.classList.contains('play'))
return iconPlay.classList.remove('play')
iconPlay.classList.add('play')
})
有
jquery
的话直接:
// jquery 添加移除 .play
$('.icon-play').click(function () {
$(this).toggleClass('play')
})
大功告成!!
如果你连JS都懒得写,只是要展示个纯CSS+HTML的demo的话,那就利用
input
标签的
:checked
属性来控制
.icon-play {
width: 100px;
height: 100px;
fill: #089;
cursor: pointer
}
.icon-play path {
d: path('M 12,26 18.5,22 18.5,14 12,10 z M 18.5,22 25,18 25,18 18.5,14 z');
transition: d .2s linear .1s
}
.toggle-play {
display: none
}
.toggle-play:checked+.icon-play path {
d: path('M 12,26 16,26 16,10 12,10 z M 21,26 25,26 25,10 21,10 z')
}
<label>
<input type="checkbox" class="toggle-play">
<svg class="icon-play" viewBox="0 0 36 36"><path/></svg>
</label>
效果图
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiZpdmL1IDN3QTOxQTM2ATMxgTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.gif)