-
Notifications
You must be signed in to change notification settings - Fork 250
/
jquery.simple-text-rotator.js
168 lines (140 loc) · 5.86 KB
/
jquery.simple-text-rotator.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* ===========================================================
* jquery-simple-text-rotator.js v1
* ===========================================================
* Copyright 2013 Pete Rojwongsuriya.
* http://www.thepetedesign.com
*
* A very simple and light weight jQuery plugin that
* allows you to rotate multiple text without changing
* the layout
* https://github.com/peachananr/simple-text-rotator
*
* ========================================================== */
!function($){
var defaults = {
animation: "dissolve",
separator: ",",
speed: 2000
};
$.fx.step.textShadowBlur = function(fx) {
$(fx.elem).prop('textShadowBlur', fx.now).css({textShadow: '0 0 ' + Math.floor(fx.now) + 'px black'});
};
$.fn.textrotator = function(options){
var settings = $.extend({}, defaults, options);
return this.each(function(){
var el = $(this)
var array = [];
$.each(el.text().split(settings.separator), function(key, value) {
array.push(value);
});
el.text(array[0]);
// animation option
var rotate = function() {
switch (settings.animation) {
case 'dissolve':
el.animate({
textShadowBlur:20,
opacity: 0
}, 500 , function() {
index = $.inArray(el.text(), array)
if((index + 1) == array.length) index = -1
el.text(array[index + 1]).animate({
textShadowBlur:0,
opacity: 1
}, 500 );
});
break;
case 'flip':
if(el.find(".back").length > 0) {
el.html(el.find(".back").html())
}
var initial = el.text()
var index = $.inArray(initial, array)
if((index + 1) == array.length) index = -1
el.html("");
$("<span class='front'>" + initial + "</span>").appendTo(el);
$("<span class='back'>" + array[index + 1] + "</span>").appendTo(el);
el.wrapInner("<span class='rotating' />").find(".rotating").hide().addClass("flip").show().css({
"-webkit-transform": " rotateY(-180deg)",
"-moz-transform": " rotateY(-180deg)",
"-o-transform": " rotateY(-180deg)",
"transform": " rotateY(-180deg)"
})
break;
case 'flipUp':
if(el.find(".back").length > 0) {
el.html(el.find(".back").html())
}
var initial = el.text()
var index = $.inArray(initial, array)
if((index + 1) == array.length) index = -1
el.html("");
$("<span class='front'>" + initial + "</span>").appendTo(el);
$("<span class='back'>" + array[index + 1] + "</span>").appendTo(el);
el.wrapInner("<span class='rotating' />").find(".rotating").hide().addClass("flip up").show().css({
"-webkit-transform": " rotateX(-180deg)",
"-moz-transform": " rotateX(-180deg)",
"-o-transform": " rotateX(-180deg)",
"transform": " rotateX(-180deg)"
})
break;
case 'flipCube':
if(el.find(".back").length > 0) {
el.html(el.find(".back").html())
}
var initial = el.text()
var index = $.inArray(initial, array)
if((index + 1) == array.length) index = -1
el.html("");
$("<span class='front'>" + initial + "</span>").appendTo(el);
$("<span class='back'>" + array[index + 1] + "</span>").appendTo(el);
el.wrapInner("<span class='rotating' />").find(".rotating").hide().addClass("flip cube").show().css({
"-webkit-transform": " rotateY(180deg)",
"-moz-transform": " rotateY(180deg)",
"-o-transform": " rotateY(180deg)",
"transform": " rotateY(180deg)"
})
break;
case 'flipCubeUp':
if(el.find(".back").length > 0) {
el.html(el.find(".back").html())
}
var initial = el.text()
var index = $.inArray(initial, array)
if((index + 1) == array.length) index = -1
el.html("");
$("<span class='front'>" + initial + "</span>").appendTo(el);
$("<span class='back'>" + array[index + 1] + "</span>").appendTo(el);
el.wrapInner("<span class='rotating' />").find(".rotating").hide().addClass("flip cube up").show().css({
"-webkit-transform": " rotateX(180deg)",
"-moz-transform": " rotateX(180deg)",
"-o-transform": " rotateX(180deg)",
"transform": " rotateX(180deg)"
})
break;
case 'spin':
if(el.find(".rotating").length > 0) {
el.html(el.find(".rotating").html())
}
index = $.inArray(el.text(), array)
if((index + 1) == array.length) index = -1
el.wrapInner("<span class='rotating spin' />").find(".rotating").hide().text(array[index + 1]).show().css({
"-webkit-transform": " rotate(0) scale(1)",
"-moz-transform": "rotate(0) scale(1)",
"-o-transform": "rotate(0) scale(1)",
"transform": "rotate(0) scale(1)"
})
break;
case 'fade':
el.fadeOut(settings.speed, function() {
index = $.inArray(el.text(), array)
if((index + 1) == array.length) index = -1
el.text(array[index + 1]).fadeIn(settings.speed);
});
break;
}
};
setInterval(rotate, settings.speed);
});
}
}(window.jQuery);