Last active
March 1, 2018 10:50
-
-
Save shisama/e7412793f7c9cf7bfb9f3ed4d4e5a9d5 to your computer and use it in GitHub Desktop.
Web Animations APIを使った波紋アニメーション(ripple effect) ref: https://qiita.com/shisama/items/c1cf3c52695ff17aff48
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animation.pause() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
el.onmouseover = function(event) { | |
animation.pause(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animation.finish() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onkeydown = function(event) { | |
if (event.key === "Escape") { | |
animation.finish(); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animation.onfinish = function() { | |
alert("finished!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animation.reverse() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onkeydown = function(event) { | |
if (event.key === "Enter") { | |
animation.reverse(); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
boxShadow: ["0 0 0 0 rgba(255,0,0, 1)", "0 0 0 20px rgba(255,0,0, 0)"], | |
background: ["red", "pink"] | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animation.play() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.querySelector(".some-button").addEventListener("click", function(event) { | |
animation.play(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animation.cancel() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.querySelector(".some-button").addEventListener("click", function(event) { | |
animation.play(); | |
setTimeout(function () { | |
animation.cancel(); | |
}, 5000); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animation.oncancel = function() { | |
alert("canceled!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment