Skip to content

Commit 31a6ad7

Browse files
Cancel previous ripple on click
1 parent 3b1d3b8 commit 31a6ad7

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

demo/demo.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,33 @@ if (!window.performance) window.performance = { now: Date.now.bind(Date) };
3131
if (!window.requestAnimationFrame) window.requestAnimationFrame = function(cb) { return setTimeout(doAnim, cb); };
3232
function doAnim(cb) { cb(performance.now()); }
3333

34-
function ripple(evt) {
35-
var button = this,
34+
function ripple(button, evt) {
35+
var request,
3636
rect = button.getBoundingClientRect(),
3737
x = evt.clientX - rect.left,
3838
y = evt.clientY - rect.top,
3939
start = performance.now();
4040
button.classList.add('animating');
41-
requestAnimationFrame(function raf(now) {
41+
request = requestAnimationFrame(function raf(now) {
4242
var count = Math.floor(now - start);
4343
button.style.cssText = '--ripple-x: ' + x + '; --ripple-y: ' + y + '; --animation-tick: ' + count + ';';
4444
if (count > 1000) {
4545
button.classList.remove('animating');
4646
button.style.cssText = '--animation-tick: 0;';
4747
return;
4848
}
49-
requestAnimationFrame(raf);
50-
})
49+
request = requestAnimationFrame(raf);
50+
});
51+
return function () {
52+
cancelAnimationFrame(request);
53+
}
5154
}
5255
[].forEach.call(document.querySelectorAll('.ripple'), function (el) {
53-
el.addEventListener('click', ripple);
54-
});
56+
var cancelRipple;
57+
el.addEventListener('click', function (evt) {
58+
if (cancelRipple) {
59+
cancelRipple();
60+
}
61+
cancelRipple = ripple(this, evt);
62+
});
63+
});

0 commit comments

Comments
 (0)