Pages

July 28, 2015

ripple effect design

As Android L material design has became a new UI inspiration, tried the same ripple effect of Android L in HTML5 and CSS3 using simple animations. So lets get started,

First, Create a button markup and bind a click event listener using jquery.
  1. <button class="ripple">AskorB</button>
Now, on clicking, we declare a ‘$div’ variable, and gets its offset. Then, we calculate the position of cursor by event.pageX and event.pageY from button position.
  1. $('.ripple').on('click', function (event) {
  2. event.preventDefault();
  3. var $div = $('<div/>'),
  4. btnOffset = $(this).offset(), //get position of div
  5. //get cursor position
  6. xPos = event.pageX - btnOffset.left,
  7. yPos = event.pageY - btnOffset.top;
  8. //add animation class
  9. $div.addClass('ripple-effect');
  10. var $ripple = $(".ripple-effect");
  11. //set the hight & width of the animation class
  12. $ripple.css("height", $(this).height());
  13. $ripple.css("width", $(this).height());
  14. $div
  15. .css({
  16. top: yPos - ($ripple.height()/2),
  17. left: xPos - ($ripple.width()/2),
  18. background: $(this).data("ripple-color")
  19. })
  20. .appendTo($(this));
  21. window.setTimeout(function(){
  22. $div.remove();
  23. }, 2000);
  24. });
Then the final part is, add CSS3 animation class for its height and width. Finish the animation is 2 seconds.
  1. @keyframes ripple-animation {
  2. from {
  3. transform: scale(1);
  4. opacity: 0.4;
  5. }
  6. to {
  7. transform: scale(100);
  8. opacity: 0;
  9. }
  10. }

No comments:

Post a Comment