/
jquery.vanish.js
133 lines (109 loc) · 3.08 KB
/
jquery.vanish.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
124
125
126
127
128
129
130
131
132
133
/**
* jQuery.vanish
*
* Creates a vanish effect vanishing random
* characters of the string in a specific time
*
* @options
* {Number} duration, the duration in milliseconds
* to vanish all string, default to 3s
* {Number} animation, the time of an item animation
* default to 300ms
* {String} className, a class to add when the
* character starts to vanish, default to 'is-vanishing'
*
* @example
* <h1 class="foo">Hello Foo Bar</h1>
*
* $('.foo').vanish({
* duration: 2500,
* animation: 200
* });
*/
;(function(factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof exports === 'object') {
factory(require('jquery'));
} else {
factory(jQuery);
}
}(function($) {
'use strict';
var defaults = {
duration: 3000,
animation: 300,
className: 'is-vanishing'
};
function Vanish(element, options) {
this.element = $(element);
this.settings = $.extend({}, defaults, options);
this.init();
}
Vanish.prototype.init = function() {
this.text = this.element.text();
this.chars = this.text.split('');
this.mount();
this.chars = this.removeEmpties(this.chars);
this.randomizables = this.indexes();
this.startTimer();
};
Vanish.prototype.mount = function() {
var elements = $(this.generate());
elements.css('transition', 'opacity ' + this.settings.animation + 'ms');
this.element.html(elements);
};
Vanish.prototype.generate = function() {
var html = '';
$.each(this.chars, function(index, char) {
if (char !== ' ') {
html += '<span>' + char + '</span>';
} else {
html += ' ';
}
});
return html;
};
Vanish.prototype.removeEmpties = function(item) {
return item.filter(function(char) {
return char !== ' ';
});
};
Vanish.prototype.indexes = function() {
return new Array(this.chars.length).join(0).split(0).map(Number.call, Number);
};
Vanish.prototype.random = function() {
return this.randomizables[Math.floor((Math.random() * this.randomizables.length))];
};
Vanish.prototype.remove = function(index) {
this.randomizables.splice(this.randomizables.indexOf(index), 1);
};
Vanish.prototype.endVanishment = function() {
clearInterval(this.settings.timer);
this.element.trigger('vanished');
};
Vanish.prototype.vanish = function() {
var sorted = this.random();
this.remove(sorted);
this.element.find('span:eq(' + sorted + ')')
.addClass(this.settings.className)
.css('opacity', 0);
if (this.randomizables.length === 0) {
this.endVanishment();
}
};
Vanish.prototype.startTimer = function() {
var self = this;
self.vanish();
this.settings.timer = setInterval(function() {
self.vanish();
}, (this.settings.duration - this.settings.animation) / (this.chars.length - 1));
};
$.fn.vanish = function(options) {
return this.each(function() {
if (!$.data(this, 'plugin_vanish')) {
$.data(this, 'plugin_vanish', new Vanish(this, options));
}
});
};
}));