mdb-ui-kit/scripts/ripples.js

331 lines
7.3 KiB
JavaScript
Raw Normal View History

2014-09-17 10:18:00 +04:00
/* Copyright 2014+, Federico Zivolo, LICENSE at https://github.com/FezVrasta/bootstrap-material-design/blob/master/LICENSE.md */
2014-11-28 11:35:17 +03:00
/* globals jQuery, navigator */
2014-11-12 13:42:40 +03:00
(function($, window, document, undefined) {
2014-12-29 16:33:55 +03:00
2014-12-28 18:36:55 +03:00
"use strict";
/**
* Define the name of the plugin
*/
2014-12-28 18:36:55 +03:00
var ripples = "ripples";
2014-11-12 13:42:40 +03:00
/**
* Get an instance of the plugin
*/
var self = null;
2014-11-28 11:35:17 +03:00
/**
* Define the defaults of the plugin
*/
var defaults = {};
2014-11-28 11:35:17 +03:00
/**
* Create the main plugin function
*/
function Ripples(element, options) {
self = this;
this.element = $(element);
this.options = $.extend({}, defaults, options);
2014-08-25 15:57:22 +04:00
this._defaults = defaults;
this._name = ripples;
this.init();
}
/**
* Initialize the plugin
*/
Ripples.prototype.init = function() {
var $element = this.element;
2014-12-28 18:36:55 +03:00
$element.on("mousedown touchstart", function(event) {
/**
* Verify if the user is just touching on a device and return if so
*/
2014-12-28 18:36:55 +03:00
if(self.isTouch() && event.type === "mousedown") {
2014-11-28 11:35:17 +03:00
return false;
}
2014-12-01 13:59:48 +03:00
/**
* Verify if the current element already has a ripple wrapper element and
2014-12-29 16:33:55 +03:00
* creates if it doesn't
*/
2014-12-28 18:36:55 +03:00
if(!($element.find(".ripple-wrapper").length)) {
$element.append("<div class=\"ripple-wrapper\"></div>");
}
/**
2014-12-29 16:33:55 +03:00
* Find the ripple wrapper
*/
var $wrapper = $element.children(".ripple-wrapper");
/**
* Get relY and relX positions
*/
var relY = self.getRelY($wrapper, event);
var relX = self.getRelX($wrapper, event);
/**
* If relY and/or relX are false, return the event
*/
if(!relY && !relX) {
return;
}
/**
* Get the ripple color
*/
var rippleColor = self.getRipplesColor();
/**
* Create the ripple element
*/
2014-12-28 18:36:55 +03:00
var $ripple = $("<div></div>");
$ripple
2014-12-29 16:33:55 +03:00
.addClass("ripple")
.css({
"left": relX,
"top": relY,
"background-color": rippleColor
});
/**
* Append the ripple to the wrapper
*/
$wrapper.append($ripple);
/**
* Make sure the ripple has the styles applied (ugly hack but it works)
*/
(function() { return window.getComputedStyle($ripple[0]).opacity; })();
/**
* Turn on the ripple animation
*/
self.rippleOn($ripple);
/**
2014-12-28 18:36:55 +03:00
* Call the rippleEnd function when the transition "on" ends
*/
setTimeout(function() {
self.rippleEnd($ripple);
}, 500);
/**
* Detect when the user leaves the element
*/
2014-12-28 18:36:55 +03:00
$element.on("mouseup mouseleave touchend", function() {
$ripple.data("mousedown", "off");
2014-12-28 18:36:55 +03:00
if($ripple.data("animating") === "off") {
self.rippleOut($ripple);
}
});
});
};
/**
* Get the new size based on the element height/width and the ripple width
*/
Ripples.prototype.getNewSize = function($ripple) {
var $element = this.element;
return (Math.max($element.outerWidth(), $element.outerHeight()) / $ripple.outerWidth()) * 2.5;
};
/**
* Get the relX
*/
Ripples.prototype.getRelX = function($wrapper, event) {
var wrapperOffset = $wrapper.offset();
if(!self.isTouch()) {
/**
* Get the mouse position relative to the ripple wrapper
*/
return event.pageX - wrapperOffset.left;
} else {
/**
2014-12-29 16:33:55 +03:00
* Make sure the user is using only one finger and then get the touch
* position relative to the ripple wrapper
*/
event = event.originalEvent;
if(event.touches.length !== 1) {
return event.touches[0].pageX - wrapperOffset.left;
2014-12-29 16:33:55 +03:00
}
return false;
}
};
/**
* Get the relY
*/
2014-12-29 16:33:55 +03:00
Ripples.prototype.getRelY = function($wrapper, event) {
var wrapperOffset = $wrapper.offset();
if(!self.isTouch()) {
/**
* Get the mouse position relative to the ripple wrapper
*/
return event.pageY - wrapperOffset.top;
} else {
/**
2014-12-29 16:33:55 +03:00
* Make sure the user is using only one finger and then get the touch
* position relative to the ripple wrapper
*/
event = event.originalEvent;
if(event.touches.length !== 1) {
return event.touches[0].pageY - wrapperOffset.top;
2014-12-29 16:33:55 +03:00
}
return false;
}
};
/**
* Get the ripple color
*/
Ripples.prototype.getRipplesColor = function() {
var $element = this.element;
2014-12-29 16:33:55 +03:00
console.log($element);
2014-12-28 18:36:55 +03:00
var color = $element.data("ripple-color") ? $element.data("ripple-color") : window.getComputedStyle($element[0]).color;
2014-12-29 16:33:55 +03:00
console.log(color);
return color;
};
/**
* Verify if the client browser has transistion support
*/
Ripples.prototype.hasTransitionSupport = function() {
var thisBody = document.body || document.documentElement;
var thisStyle = thisBody.style;
var support = (
2014-12-29 16:33:55 +03:00
thisStyle.transition !== undefined ||
thisStyle.WebkitTransition !== undefined ||
thisStyle.MozTransition !== undefined ||
thisStyle.MsTransition !== undefined ||
thisStyle.OTransition !== undefined
);
return support;
};
/**
* Verify if the client is using a mobile device
*/
Ripples.prototype.isTouch = function() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
};
/**
* End the animation of the ripple
*/
Ripples.prototype.rippleEnd = function($ripple) {
2014-12-28 18:36:55 +03:00
$ripple.data("animating", "off");
2014-12-28 18:36:55 +03:00
if($ripple.data("mousedown") === "off") {
self.rippleOut($ripple);
}
};
/**
* Turn off the ripple effect
*/
Ripples.prototype.rippleOut = function($ripple) {
$ripple.off();
if(self.hasTransitionSupport()) {
2014-12-28 18:36:55 +03:00
$ripple.addClass("ripple-out");
} else {
2014-12-28 18:36:55 +03:00
$ripple.animate({"opacity": 0}, 100, function() {
$ripple.trigger("transitionend");
});
}
2014-12-28 18:36:55 +03:00
$ripple.on("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function() {
$ripple.remove();
});
};
/**
* Turn on the ripple effect
*/
Ripples.prototype.rippleOn = function($ripple) {
var size = self.getNewSize($ripple);
var $element = this.element;
if(self.hasTransitionSupport()) {
$ripple
2014-12-29 16:33:55 +03:00
.css({
"-ms-transform": "scale(" + size + ")",
"-moz-transform": "scale(" + size + ")",
"-webkit-transform": "scale(" + size + ")",
"transform": "scale(" + size + ")"
})
.addClass("ripple-on")
.data("animating", "on")
.data("mousedown", "on");
} else {
$ripple.animate({
2014-12-28 18:36:55 +03:00
"width": Math.max($element.outerWidth(), $element.outerHeight()) * 2,
"height": Math.max($element.outerWidth(), $element.outerHeight()) * 2,
"margin-left": Math.max($element.outerWidth(), $element.outerHeight()) * (-1),
"margin-top": Math.max($element.outerWidth(), $element.outerHeight()) * (-1),
"opacity": 0.2
}, 500, function() {
2014-12-28 18:36:55 +03:00
$ripple.trigger("transitionend");
});
}
};
/**
2014-12-29 16:33:55 +03:00
* Create the jquery plugin function
*/
$.fn.ripples = function(options) {
return this.each(function() {
2014-12-28 18:36:55 +03:00
if(!$.data(this, "plugin_" + ripples)) {
$.data(this, "plugin_" + ripples, new Ripples(this, options));
}
});
};
})(jQuery, window, document);