Simple JS/CSS Ripple effect on button
Created
October 20, 2020 12:36
-
-
Save GGrassiant/3173f6ac98c37e8441116fd4c96cbcfd to your computer and use it in GitHub Desktop.
Simple JS/CSS Ripple effect on button
This file contains hidden or 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
| <!doctype html> | |
| <html class="no-js" lang=""> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title></title> | |
| <meta name="description" content=""> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <meta property="og:title" content=""> | |
| <meta property="og:type" content=""> | |
| <meta property="og:url" content=""> | |
| <meta property="og:image" content=""> | |
| <link rel="manifest" href="site.webmanifest"> | |
| <link rel="apple-touch-icon" href="icon.png"> | |
| <!-- Place favicon.ico in the root directory --> | |
| <link rel="stylesheet" href="css/normalize.css"> | |
| <link rel="stylesheet" href="css/main.css"> | |
| <meta name="theme-color" content="#fafafa"> | |
| </head> | |
| <body> | |
| <!-- Add your site or application content here --> | |
| <button>Find out more</button> | |
| <script src="js/vendor/modernizr-3.11.2.min.js"></script> | |
| <script src="js/plugins.js"></script> | |
| <script src="js/main.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or 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
| class Button { | |
| constructor(HTMLButtonElement) { | |
| this.button = HTMLButtonElement; | |
| this.left = this.button.offsetLeft; | |
| this.top = this.button.offsetTop; | |
| this.createRipple(); | |
| } | |
| createRipple() { | |
| this.button.addEventListener("click", (event) => { | |
| const circle = document.createElement("span"); | |
| const diameter = Math.max( | |
| this.button.clientWidth, | |
| this.button.clientHeight | |
| ); | |
| const radius = diameter / 2; | |
| const offsetLeft = event.clientX - this.left; | |
| const offsetTop = event.clientY - this.top; | |
| circle.style.width = circle.style.height = `${diameter}px`; | |
| circle.style.left = `${offsetLeft - radius}px`; | |
| circle.style.top = `${offsetTop - radius}px`; | |
| circle.classList.add("ripple"); | |
| const ripple = this.button.getElementsByClassName("ripple")[0]; | |
| if (ripple) { | |
| ripple.remove(); | |
| } | |
| this.button.appendChild(circle); | |
| }); | |
| }; | |
| } | |
| const buttons = document.getElementsByTagName("button"); | |
| for (const button of buttons) { | |
| new Button(button); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment