A simple way to make your own button ripple effect, similar to that found in Google’s material design.
Code can be found here: https://github.com/GeekLaunch/button-ripple-effect
Live demo: https://geeklaunch.github.io/button-ripple-effect/index.html
Note that this code is my own original creation, not copied from any external source.
GeekLaunch produces educational videos covering such topics as Linux, web development (including HTML5, CSS 3, JavaScript, and PHP), Java programming, tips for power users, among many others. Subscribe: New video every Wednesday!
Not a geek? Start today!
Original source
40 responses to “Google-like BUTTON RIPPLE EFFECT in Pure CSS & JavaScript Tutorial”
I prefer pageX and pageY instead of clientX/Y. I mean:
When
circle.style.left = e.clientX – this.offsetLeft – d / 2 + "px";
circle.style.top = e.clientY – this.offsetTop – d / 2 + "px";
it takes width and height of an screen so when you'll scroll down it will drop coordinates (because screen have always the same width and height – only page height and width is changing) and give false value.
When you do it like this:
circle.style.left = e.pageX – screenLeft – this.offsetLeft – d / 2 + "px";
circle.style.top = e.pageY – screenTop – this.offsetTop – d / 2 + "px";
you can scroll down and everything works.
Thanks for this video – helped me so much.
Google has now made this public to everyone. Include "<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>" in <head>. If you would like to add the ripple to anything, follow these steps:
1. Find an object to add a ripple to.
<button class="one-class another-class even-another-class">Hello World</button>
2. Include "mdl-js-ripple-effect"
<button class="one-class another-class even-another-class mdl-js-ripple-effect">Hello World</button>
That's all (I've added a link to a codepen; https://codepen.io/Tizen20/pen/Bgepzz?editors=1000#0)
If hte button is in the right of window, wave doesnt work:(
there is a way to make it in powerpoint?!
What are you using to get your atom to look like that?
Bro it's awesome!
Thank you soo much)
What if i want to use this effect for input buttons. How can i make that work aswell?
what theme you are using
First of all, I would like to thank you for making this video as this is the only way that worked for me and isn't a gigantic library that adds unnecessary loading time but still is easy to customize.
It all works great for me on buttons / simple boxes however not on text elements that are styled like buttons:
I already found a way to fix a problem where the size of the effect would be 0*0px (marked in the codepen) but I have no clue how to stop the effect from leaving the button itself.
I'd really appreciate if you could take a look at my problem and maybe help me fix it 😉
https://codepen.io/anon/pen/ddJMVN
Also I've modified it to use jQuery, slightly changed the animation and added an auto-removal for the divs.
U should zoom ur video when u r going to type
Thks its helpful i cant wait to subscribe
I use this code
function gota(e){
var w=e.pageX-boton.offsetLeft;
var h=e.pageY-boton.offsetTop;
var c=document.createElement("div");
var d=boton.appendChild(c);
d.setAttribute("class","efecto");
var pox=e.pageX-boton.offsetLeft;
var poy=e.pageY-boton.offsetTop;
d.style.left=pox+"px";
d.style.top=poy+"px";
setTimeout(function(){
boton.removeChild(c);
},3000);
}
Great Tutorial!! Helped me a lot, thanks! ☺
Hey just a quick question, what if one wants the circle's center to align with that of the button being clicked irrespective of what part of the button is clicked?
How can i link somethink to this button?
Thank you!
circle.style.width = circle.style.height = d + 'px';
var rect = this.getBoundingClientRect();
circle.style.left = e.clientX – rect.left -d/2 + 'px';
circle.style.top = e.clientY – rect.top – d/2 + 'px';
a little more explanation of these positioning and sizing please ^_^
You know what I love you, bro, you actually taught me what I was seeking God bless you man thanks so much <3
Great tutorial, I made a small module out of it which will turn any button sent to it into a ripple button. I used the setTimeout function to terminate the childnodes. https://github.com/JohanSundman/ripple-button
I'm getting an Uncaught TypeError at this.appendChild – this.appendChild is not a function. Help.
Really good tutorial! It helped me a lot. Just 1 suggesstion: Delete the "Pure" before CSS. If it's css and js, it can't be pure css.
Finally!! thanks bro
You explained that very well. Still getting to learn Javascript, but this helped me take another step in the right direction. Thanks man!
Good job! I used this to eliminate the 'divs':
function createRipple(e)
{
if(this.getElementsByClassName('ripple').length > 0)
{
this.removeChild(this.childNodes[1]);
}
var circle = document.createElement('div');
this.appendChild(circle);
var d = Math.max(this.clientWidth, this.clientHeight);
circle.style.width = circle.style.height = d + 'px';
circle.style.left = e.clientX – this.offsetLeft – d / 2 + 'px';
circle.style.top = e.clientY – this.offsetTop – d / 2 + 'px';
circle.classList.add('ripple');
}
See in: https://codepen.io/campao/pen/EmPvbM
ziehne
This was so well explained
How can we do all this in Angular JS where i can change the color of my button in a ripple formation way permanently.
Thank you very much for your hard work!
link href="asdfsdf"? cant see that clearly. it doesnt matter right?
Can you tell me the extensions you use for Atom? Especially the extension that shows a quick small preview of the file at the top right.
you are awesome and thank you very very much, your explain is very helpful
Do you have skype? I am having a issue where the ripple is going in the middle of the page instead of in the button
Really cool tutorial!
Great tutorial! thank you so much!
Great Tutorial !
It would be great if you make tutorial of comment box using AJAX!
if only this worked.
Nice and perfect tutorial. please help if we write this code in any div button ripple is not working in below code
<div id="wrapper">
<form>
<button>submit</button>
</form>
</div>
it creates many divs and i would like that stop it, also i would like to know how i can get the exact point which user click. ty
Beautiful text editor ya got there.
Thank you so much for this tutorial. It helped me A LOT!
i used:
var element = e.target;
var rect = element.getBoundingClientRect();
circle.style.left = e.clientX – rect.left -d/2 + 'px';
circle.style.top = e.clientY – rect.top – d/2 + 'px';
because that coordinades in the code didnt work for me.. hope it helps someone