Created
April 25, 2017 10:11
-
-
Save mrxf/abb0a41ee2258b2b173cc739070816c3 to your computer and use it in GitHub Desktop.
material ripple effect
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
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