forked from MikeCostello/jquery-slide-text-left
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.slide-text-left.js
99 lines (83 loc) · 2.59 KB
/
jquery.slide-text-left.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
/*
Copyright 2013 Michael Costello.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Michael Costello ([email protected])
*/
(function($) {
/*
* Tranistions text by clipping to the left, then expanding to
* reveal the next text.
*
* @example .slideTextLeft(word, [delay])
* @param {String} a single word to tranistion to once
* @param {Number} the time to wait before the transistion
*
* @example .slideTextLeft(words, [delay])
* @param {Array} multiple words to tranistion through in a loop
* @param {Number} the time to wait before the transistion
*
* @example .slideTextLeft(settings)
* @param {Object} setting object with "words" & optionally "delay" members
*
*/
$.fn.slideTextLeft = function(words, delay) {
var isSingleWord = false;
// map arguments
var opts = $.fn.slideTextLeft.defaults;
if (typeof delay === "number") {
opts.delay = delay;
}
if ($.isArray(words)) {
opts.words = words;
} else if (typeof words === "string") {
opts.words = words;
isSingleWord = true;
} else if ($.isPlainObject(words)) {
opts = $.extend({}, $.fn.slideTextLeft.defaults, words);
}
// iterate and reformat each matched element
return this.each(function() {
var $this = $(this);
var initialText = $this.text();
var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
var i = 0;
if (!o.words.length) {
return;
}
$this.css({
"white-space": "nowrap",
"overflow": "hidden",
"vertical-align": "bottom"
});
if (!initialText.length || initialText === o.words[0]) {
$this.text(o.words[0]);
i = 1;
}
// start animation
(function loop() {
var word = isSingleWord ? o.words : o.words[i];
var callback = isSingleWord ? null : loop;
transitionText(word, $this, o.delay, callback);
i = (i + 1) % o.words.length;
})();
});
};
function transitionText(text, $this, delay, callback) {
$this.delay(delay)
.animate({ "width": "toggle" }, $.proxy($this.text, $this, text))
.animate({ "width": "toggle" }, callback);
}
// plugin defaults
$.fn.slideTextLeft.defaults = {
words: [],
delay: 2000
};
})(jQuery);