-
Notifications
You must be signed in to change notification settings - Fork 1
/
nc-ripple.js
113 lines (92 loc) · 3.5 KB
/
nc-ripple.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
angular.module('nc.ripple', [])
.directive("ncRipple", function() {
return {
restrict: 'A',
scope: {
rOpacity: '@',
rSize: '@',
rDuration: '@',
rColor: '@'
},
link: function(scope, element, attrs) {
var COLOR_REGEXP = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$";
var cutHex = function(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
var hexToR = function(h) {return parseInt((cutHex(h)).substring(0,2),16)}
var hexToG = function(h) {return parseInt((cutHex(h)).substring(2,4),16)}
var hexToB = function(h) {return parseInt((cutHex(h)).substring(4,6),16)}
/* Initailizing */
element.css('overflow', 'hidden');
element.css('transform', 'translateZ(0)');
if(scope.rOpacity && scope.rOpacity >= 0 && scope.rOpacity <=1)
;
else
scope.rOpacity = '.5';
if(!scope.rSize)
scope.rSize = 200;
if(!scope.rDuration)
scope.rDuration=1.5;
scope.rDurationS = scope.rDuration +'s';
/* End of initializing */
var r=220,g=220,b=220;
var validates = new RegExp(COLOR_REGEXP);
if (scope.rColor && validates.test(scope.rColor)){
r=hexToR(scope.rColor);
g=hexToG(scope.rColor);
b=hexToB(scope.rColor);
}
var x, y=0, size={},
offsets,
func = function(e) {
var ripple = this.querySelector('b.drop');
if (ripple == null) {
// Create ripple
ripple = document.createElement('b');
ripple.className += 'drop';
// Prepend ripple to element
this.insertBefore(ripple, this.firstChild);
ripple.style.background = 'rgba(' + r + ',' + g +',' + b + ',' + scope.rOpacity + ')'
ripple.style.height=scope.rSize+'px';
ripple.style.width=scope.rSize+'px';
size.x = ripple.offsetWidth;
size.y = ripple.offsetHeight;
}
var eventType = e.type;
// Remove animation effect
ripple.style.animationDuration=null;
ripple.style.oAnimationDuration=null;
ripple.style.webkitAnimationDuration=null;
ripple.style.mozAnimationDuration=null;
ripple.style.msAnimationDuration=null;
ripple.className = ripple.className.replace(/ ?(animate)/g, '');
// get click coordinates by event type
x = e.pageX;
y = e.pageY;
// set new ripple position by click or touch position
function getPos(element) {
var de = document.documentElement;
var box = element.getBoundingClientRect();
var top = box.top + window.pageYOffset - de.clientTop;
var left = box.left + window.pageXOffset - de.clientLeft;
return {
top: top,
left: left
};
}
offsets = getPos(element[0]);
innerPos = {};
innerPos.x = e.pageX;
innerPos.y = e.pageY;
ripple.style.left = (innerPos.x - offsets.left - size.x / 2) + 'px';
ripple.style.top = (innerPos.y - offsets.top - size.y / 2) + 'px';
// Add animation effect
ripple.style.animationDuration=scope.rDurationS;
ripple.style.oAnimationDuration=scope.rDurationS;
ripple.style.webkitAnimationDuration=scope.rDurationS;
ripple.style.mozAnimationDuration=scope.rDurationS;
ripple.style.msAnimationDuration=scope.rDurationS;
ripple.className += ' animate';
}
element.on("mouseup", func);
}
}
})