Skip to content

Instantly share code, notes, and snippets.

@shisama
Last active March 1, 2018 10:50
Show Gist options
  • Save shisama/e7412793f7c9cf7bfb9f3ed4d4e5a9d5 to your computer and use it in GitHub Desktop.
Save shisama/e7412793f7c9cf7bfb9f3ed4d4e5a9d5 to your computer and use it in GitHub Desktop.
Web Animations APIを使った波紋アニメーション(ripple effect) ref: https://qiita.com/shisama/items/c1cf3c52695ff17aff48
const el = document.querySelector(".pulsator");
const style = {
width: "15px",
height: "15px",
borderRadius: "50%",
borderColor: "red",
background: "red",
boxShadow: "0 0 0 rgba(255,0,0, 0.4)"
};
Object.assign(element.style, style);
const el = document.querySelector(".pulsator");
const animation = el.animate(
{
boxShadow: ["0 0 0 0 rgba(255,0,0, 1)", "0 0 0 20px rgba(255,0,0, 0)"]
},
{
duration: 1500,
iterations: Infinity
}
);
animation.pause()
el.onmouseover = function(event) {
animation.pause();
};
animation.finish()
window.onkeydown = function(event) {
if (event.key === "Escape") {
animation.finish();
}
};
animation.onfinish = function() {
alert("finished!");
}
animation.reverse()
window.onkeydown = function(event) {
if (event.key === "Enter") {
animation.reverse();
}
};
{
boxShadow: ["0 0 0 0 rgba(255,0,0, 1)", "0 0 0 20px rgba(255,0,0, 0)"],
background: ["red", "pink"]
},
const animation = el.animate([
{
boxShadow: "0 0 0 0 rgba(255,0,0, 1)",
background: "red"
},
{
boxShadow: "0 0 0 20px rgba(255,0,0, 0)",
background: "pink"
}
],
{
duration: 1500,
iterations: Infinity
}
);
const keyframes = new KeyframeEffect(
el,
{
boxShadow: ["0 0 0 0 rgba(255,0,0, 1)", "0 0 0 20px rgba(255,0,0, 0)"]
},
{
duration: 1500,
iterations: Infinity
}
);
const animation = new Animation(keyframes, document.timeline);
animation.play()
document.querySelector(".some-button").addEventListener("click", function(event) {
animation.play();
});
animation.cancel()
document.querySelector(".some-button").addEventListener("click", function(event) {
animation.play();
setTimeout(function () {
animation.cancel();
}, 5000);
});
animation.oncancel = function() {
alert("canceled!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment