-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple-ripple.js
71 lines (66 loc) · 2.94 KB
/
simple-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
(function ($) {
/**
A simple ripple effect, material design like jQuery plugin to beautify your UI component. jQuery水纹效果插件
How to use it? 使用方式
$('.ripple-effect').ripple({
speed: 200, // the wave's speed 水纹速度
color: '#fff' // the wave's color 水纹颜色
}
});
*/
$.fn.ripple = function (options) {
var defaults = {
speed: 200,
color: '#000'
};
var supportedTouch = true;
try { ontouchstart; } catch (e) { supportedTouch = false; }
options = $.extend({}, defaults, options);
this.each(function () {
var This = $(this),
tpl = '<span class="ripple-container"><span class="ripple is-animated"></span></span>';
This.append(tpl);
var $r = This.find('.ripple'),
width = This.width(),
height = This.height();
if (supportedTouch) {
This.on('touchstart', function (e) {
var clientX = e.originalEvent.touches[0].clientX,
clientY = e.originalEvent.touches[0].clientY;
press(clientX, clientY);
});
This.on('touchend', release);
} else {
This.on('mousedown', function (e) {
var clientX = e.clientX || e.pageX,
clientY = e.clientY || e.pageY;
press(clientX, clientY);
});
This.on('mouseup', release);
}
function press(clientX, clientY) {
var radius = Math.pow(width * width + height * height, 0.5) * 2;
clientX = clientX - This.offset().left;
clientY = clientY - This.offset().top;
$r.removeClass('is-animated').addClass('is-visible').css({
background: options.color,
width: radius + 'px',
height: radius + 'px',
transform: 'translate(-50%, -50%) translate(' + clientX + 'px,' + clientY + 'px) ' + 'scale(0.0001, 0.0001)',
'-webkit-transform': 'translate(-50%, -50%) translate(' + clientX + 'px,' + clientY + 'px) ' + 'scale(0.0001, 0.0001)'
});
setTimeout(function () {
$r.addClass('is-animated').css({
transform: 'translate(-50%, -50%) translate(' + clientX + 'px,' + clientY + 'px)',
'-webkit-transform': 'translate(-50%, -50%) translate(' + clientX + 'px,' + clientY + 'px)'
});
}, 10);
}
function release() {
setTimeout(function () {
$r.removeClass('is-visible');
}, options.speed);// the wave's speed 这里控制水纹的速度
}
});
}
})(jQuery);