material-ripples.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Copyright 2014+, Federico Zivolo, LICENSE at https://github.com/FezVrasta/bootstrap-material-design/blob/master/LICENSE.md */
  2. /* globals jQuery, navigator */
  3. (function($) {
  4. // Detect if the browser supports transitions
  5. $.support.transition = (function(){
  6. var thisBody = document.body || document.documentElement,
  7. thisStyle = thisBody.style,
  8. support = (
  9. thisStyle.transition !== undefined ||
  10. thisStyle.WebkitTransition !== undefined ||
  11. thisStyle.MozTransition !== undefined ||
  12. thisStyle.MsTransition !== undefined ||
  13. thisStyle.OTransition !== undefined
  14. );
  15. return support;
  16. })();
  17. $.ripples = function(options) {
  18. // Default options
  19. var defaultOptions = {
  20. "target": ".btn:not(.btn-link), .card-image, .navbar a:not(.withoutripple), .nav-tabs a:not(.withoutripple), .withripple"
  21. };
  22. function isTouch() {
  23. return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
  24. }
  25. // Fade out the ripple and then destroy it
  26. function rippleOut(ripple) {
  27. // Unbind events from ripple
  28. ripple.off();
  29. // Start the out animation
  30. if ($.support.transition) {
  31. ripple.addClass("ripple-out");
  32. } else {
  33. ripple.animate({
  34. "opacity": 0
  35. }, 100, function() {
  36. ripple.trigger("transitionend");
  37. });
  38. }
  39. // This function is called when the transition "out" ends
  40. ripple.on("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){
  41. ripple.remove();
  42. });
  43. }
  44. // Apply custom options
  45. options = $.extend(defaultOptions, options);
  46. $(document)
  47. .on("mousedown touchstart", options.target, function(e) {
  48. if (isTouch() && e.type == "mousedown") {
  49. return false;
  50. }
  51. var element = $(this);
  52. // If the ripple wrapper does not exists, create it
  53. if (!$(this).find(".ripple-wrapper").length) {
  54. $(this).append("<div class=ripple-wrapper></div>");
  55. }
  56. var wrapper = $(this).find(".ripple-wrapper");
  57. var wrapperOffset = wrapper.offset(),
  58. relX,
  59. relY;
  60. if (!isTouch()) {
  61. // Get the mouse position relative to the ripple wrapper
  62. relX = e.pageX - wrapperOffset.left;
  63. relY = e.pageY - wrapperOffset.top;
  64. } else {
  65. // Make sure the user is using only one finger and then get the touch position relative to the ripple wrapper
  66. e = e.originalEvent;
  67. if (e.touches.length === 1) {
  68. relX = e.touches[0].pageX - wrapperOffset.left;
  69. relY = e.touches[0].pageY - wrapperOffset.top;
  70. } else {
  71. return;
  72. }
  73. }
  74. // Meet the new ripple
  75. var ripple = $("<div></div>");
  76. // Add to it the ripple class
  77. ripple.addClass("ripple");
  78. // Position it in the right place
  79. ripple.css({"left": relX, "top": relY});
  80. // Set the background color of the ripple
  81. ripple.css({"background-color": window.getComputedStyle($(this)[0]).color});
  82. // Spawn it
  83. wrapper.append(ripple);
  84. // Make sure the ripple has the styles applied (ugly hack but it works)
  85. (function() { return window.getComputedStyle(ripple[0]).opacity; })();
  86. // Set the new size
  87. var size = (Math.max($(this).outerWidth(), $(this).outerHeight()) / ripple.outerWidth()) * 2.5;
  88. // Decide if use CSS transitions or jQuery transitions
  89. if ($.support.transition) {
  90. // Start the transition
  91. ripple.css({
  92. "-ms-transform": "scale(" + size + ")",
  93. "-moz-transform": "scale(" + size + ")",
  94. "-webkit-transform": "scale(" + size + ")",
  95. "transform": "scale(" + size + ")"
  96. });
  97. ripple.addClass("ripple-on");
  98. ripple.data("animating", "on");
  99. ripple.data("mousedown", "on");
  100. } else {
  101. // Start the transition
  102. ripple.animate({
  103. "width": Math.max($(this).outerWidth(), $(this).outerHeight()) * 2,
  104. "height": Math.max($(this).outerWidth(), $(this).outerHeight()) * 2,
  105. "margin-left": Math.max($(this).outerWidth(), $(this).outerHeight()) * -1,
  106. "margin-top": Math.max($(this).outerWidth(), $(this).outerHeight()) * -1,
  107. "opacity": 0.2
  108. }, 500, function() {
  109. ripple.trigger("transitionend");
  110. });
  111. }
  112. // This function is called when the transition "on" ends
  113. setTimeout(function() {
  114. ripple.data("animating", "off");
  115. if (ripple.data("mousedown") == "off") {
  116. rippleOut(ripple);
  117. }
  118. }, 500);
  119. // On mouseup or on mouseleave, set the mousedown flag to "off" and try to destroy the ripple
  120. element.on("mouseup mouseleave", function() {
  121. ripple.data("mousedown", "off");
  122. // If the transition "on" is finished then we can destroy the ripple with transition "out"
  123. if (ripple.data("animating") == "off") {
  124. rippleOut(ripple);
  125. }
  126. });
  127. });
  128. };
  129. $.fn.ripples = function() {
  130. $.ripples({"target": $(this)});
  131. };
  132. })(jQuery);