您正在寻找的解决方案是javascript 或明智地使用css-keyframes:
在您的 codepen 示例中 replace everything inside you css with this code:
test {
background-color: blue;
width:346px;
height:213px;
clip-path: polygon(323.19px 0.00px, 0.00px 186.65px, 0.00px 213.00px, 346.00px 13.17px, 323.19px 0.00px);
}
.animate {
animation-duration: 3s;
animation-name: animtest;
animation-delay: 0;
animation-iteration-count: infinite;
animation-direction: forward;
}
.reverseanimate {
animation-duration: 3s;
animation-name: animtest;
animation-delay: 0;
animation-iteration-count: infinite;
animation-direction: reverse;
}
@keyframes animtest {
0% {
clip-path: polygon(323.19px 0.00px, 0.00px 186.65px, 0.00px 213.00px, 346.00px 13.17px, 323.19px 0.00px)
}
100% {
clip-path: polygon(0.00px 168.65px, 0.00px 229.00px, 345.00px 30.17px, 292.64px 0.00px, 0.00px 168.65px)
}
}
那么,replace everything inside you js with this code:
let target = document.getElementById('test');
setInterval(function(){
if ($(target).hasClass("animate")) {
$(target).removeClass("animate");
$(target).addClass("reverseanimate");
}
else {
$(target).addClass("animate");
$(target).removeClass("reverseanimate");
}
}, 3000);
注意:由于某种原因,我无法在 codepen 中创建帐户