Skip to content

Instantly share code, notes, and snippets.

@mrxf
Created April 25, 2017 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrxf/abb0a41ee2258b2b173cc739070816c3 to your computer and use it in GitHub Desktop.
Save mrxf/abb0a41ee2258b2b173cc739070816c3 to your computer and use it in GitHub Desktop.
material ripple effect
function RippleEffect(element){
this.element = element;
this.element.addEventListener('mousedown', this.run.bind(this), false);
}
RippleEffect.prototype = {
run: function(event){
var ripplerContainer = this.element.querySelector('.ripple-container');
var offsetInfo = this.element.getBoundingClientRect();
var rippleContainer = document.createElement('div');
rippleContainer.style.position = 'fixed';
rippleContainer.style.zIndex = 99;
rippleContainer.style.width = offsetInfo.width + 'px';
rippleContainer.style.left = offsetInfo.left + 'px';
rippleContainer.style.top = offsetInfo.top + 'px';
rippleContainer.style.height = offsetInfo.height + 'px';
rippleContainer.className = 'ripple-container';
rippleContainer.style.overflow = 'hidden';
this.element.appendChild(rippleContainer);
var maxLength = offsetInfo.width > offsetInfo.height ? offsetInfo.width : offsetInfo.height;
var circleD = maxLength * 2;
var ripple = document.createElement('div');
ripple.style.position = 'absolute';
ripple.style.width = circleD + 'px';
ripple.style.height = circleD + 'px';
ripple.style.borderRadius = '50%';
ripple.style.left = ((event.pageX - offsetInfo.left) - circleD/2) + 'px';
ripple.style.top = ((event.pageY - offsetInfo.top) - circleD/2) + 'px';
ripple.className = 'ripple';
rippleContainer.appendChild(ripple);
ripple.addEventListener('animationend', function(){
rippleContainer.remove();
}.bind(this), false);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment