-
Notifications
You must be signed in to change notification settings - Fork 4
/
jquery.strokeanimation.js
123 lines (103 loc) · 3.48 KB
/
jquery.strokeanimation.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
114
115
116
117
118
119
120
121
122
123
/**
Stroke Animation Plugin - v1.0
@author Yu Ishihara
@github https://github.com/is8r/jquery-strokeanimation
@version 1.0
@howto
;(function(jQuery) {
$(function(){
$('.lines').strokeanimation({
interval: 5000,
loop: true,
//autoStart: false,
color: '#ffffff'
});
//$('.lines').data('strokeanimation').enter();
});
})(jQuery);
*/
;(function(jQuery) {
var pluginName = 'strokeanimation';
$[pluginName] = function(element, options) {
//----------------------------------------------------------------------
//data
//data for plugin
var defaults = {
pluginName: 'strokeanimation',
color: null,
loop: true,
autoStart: false,
interval: 3000
}
var plugin = this;
plugin.settings = {}
var $element = $(element);
var element = element;
//----------------------------------------------------------------------
//method
plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
plugin.items = options.items;
plugin.start();
}
plugin.start = function(e) {
var path,
svg;
//init
$element.find('path').each(function(i, el) {
el.setAttribute('stroke-dashoffset', '0');
if(plugin.settings.color) el.attributes.getNamedItem('stroke').nodeValue = plugin.settings.color;
});
$element.find('circle').each(function(i, el) {
el.setAttribute('stroke-dashoffset', '0');
if(plugin.settings.color) el.attributes.getNamedItem('stroke').nodeValue = plugin.settings.color;
});
//start
if(plugin.settings.autoStart) {
plugin.enter();
}
if(plugin.settings.loop) {
setInterval(plugin.enter, plugin.settings.interval);
}
}
plugin.enter = function(e) {
var length,
i;
//visible
element.style.display = 'block';
//refresh and enter
$element.find('path').each(function(i, el) {
el.style.transition = el.style.WebkitTransition = 'none';
length = el.getTotalLength();
el.style.strokeDasharray = length + ' ' + length;
el.style.strokeDashoffset = length;
el.getBoundingClientRect();
el.style.transition = el.style.WebkitTransition = 'stroke-dashoffset 3s ease-in-out';
el.style.strokeDashoffset = '0';
});
$element.find('circle').each(function(i, el) {
el.style.transition = el.style.WebkitTransition = 'none';
length = el.attributes.getNamedItem('r').nodeValue*10;
el.style.strokeDasharray = length + ' ' + length;
el.style.strokeDashoffset = length;
el.getBoundingClientRect();
el.style.transition = el.style.WebkitTransition = 'stroke-dashoffset 3s ease-in-out';
el.style.strokeDashoffset = '0';
});
}
plugin.init();
}
//----------------------------------------------------------------------
$.fn[pluginName] = function(options) {
if(!options) options = {};
options.items = [];
return this.each(function(i) {
options.id = i;
options.items.push($(this));
if (undefined == $(this).data(pluginName)) {
var plugin = new $[pluginName](this, options);
$(this).data(pluginName, plugin);
}
});
}
})(jQuery);