闽公网安备 35020302035485号
btn.addEventListener('click', _.throttle(save, 300))
其实除了 JS 方式, CSS 也可以非常轻易实现这样一个功能,无需任何框架库,一起看看吧!
<button onclick="console.log('保存')">保存</button>
这时的按钮连续点击就会不断地触发,效果如下
@keyframes throttle {
from {
pointer-events: none;
}
to {
pointer-events: all;
}
}
很简单吧,就是从禁用到可点击的变化。接下来,将这个动画绑定在按钮上,这里为了方便测试,将动画设置成了2sbutton{
animation: throttle 2s step-end forwards;
}
注意,这里动画的缓动函数设置成了阶梯曲线,step-end,它可以很方便控制pointer-events的变化时间点。如下示意,pointer-events在0~2秒内的值都是none,一旦到达2秒,就立刻变成了all,由于是forwards,会一直保持all的状态
button:active{
animation: none;
}
为了演示方便,我们暂时把颜色变化也加在动画里@keyframes throttle {
from {
color: red;
pointer-events: none;
}
to {
color: green;
pointer-events: all;
}
}
现在如果文字是red,表示是禁用态,只有是green,才表示可以被点击,非常清晰明了,如下:

/* === 堆代码 www.duidaima.com ===*/
button{
animation: throttle 2s step-end forwards;
}
button:active{
animation: none;
}
@keyframes throttle {
from {
pointer-events: none;
}
to {
pointer-events: all;
}
}
三、总结一下