-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjstyper.js
178 lines (166 loc) · 4.95 KB
/
jstyper.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
167
168
169
170
171
172
173
174
175
176
177
178
/**
* jsTyper - Pure Javascript Typing Slider, v1.0
* Copyright 2013 Mohsen Khahani
*
* Licensed under the MIT license
* Created on May 18, 2013
*
* Features:
* - Nice typing effects
* - Support XHTML slides
* - Configurable typing speed
* - Pause on mouse over
*
* http://mohsenkhahani.ir/jsTyper
*/
/**
* Default configuration options
*/
var jsTyperOptions = {
slideDelay : 2000, /* Time delay between slides in millisecond */
typeDelay : 100, /* Typing delay in millisecond */
blinking : 2, /* Number of cursor blinkings before typing starts */
cursor : '_', /* The trailing text that simulates the cursor */
loop : true /* Looping over slides */
};
/**
* jsTyper base class
*/
function jsTyper(elementId, options) {
options = options || {};
this.typeDelay = (options.typeDelay !== undefined)? options.typeDelay : jsTyperOptions.typeDelay;
this.slideDelay = (options.slideDelay !== undefined)? options.slideDelay : jsTyperOptions.slideDelay;
this.blinking = (options.blinking !== undefined)? options.blinking : jsTyperOptions.blinking;
this.cursor = (options.cursor !== undefined)? options.cursor : jsTyperOptions.cursor;
this.loop = (options.loop !== undefined)? options.loop : jsTyperOptions.loop;
this.slides = [];
this.timer = null;
this.typing = false;
this.playing = false;
this.currIdx = 0;
this.init(elementId);
}
/**
* Initiates typer
*/
jsTyper.prototype.init = function(id) {
function getInnerElement(el) {
if (el.children.length === 0) {
return el;
} else {
return getInnerElement(el.children[0]);
}
}
var self = this,
slider,
i;
if (document.readyState != 'complete') {
setTimeout(function() { self.init(id); }, 100);
return;
}
slider = this.slider = document.getElementById(id);
if (slider.children.length === 0) {
return;
}
for (i = 0; i < slider.children.length; i++) {
this.slides[i] = {};
this.slides[i].outer = slider.children[i];
this.slides[i].outer.style.display = 'none';
this.slides[i].inner = getInnerElement(slider.children[i]);
this.slides[i].text = this.slides[i].inner.innerHTML;
}
this.addEvents(slider);
slider.style.display = '';
this.playing = true;
this.play();
};
/**
* Starts typer
*/
jsTyper.prototype.play = function() {
var slide = this.slides[this.currIdx],
prevIdx = (this.currIdx === 0) ?
this.slides.length - 1 : this.currIdx - 1;
if (this.playing && !this.typing) {
this.typing = true;
this.slides[prevIdx].outer.style.display = 'none';
this.blink(slide, 0);
if (this.currIdx === this.slides.length - 1) {
if (this.loop) {
this.currIdx = 0;
} else {
this.playing = false;
this.removeEvents(this.slider);
return;
}
} else {
this.currIdx += 1;
}
}
};
/**
* Blinks cursor before type begins
*/
jsTyper.prototype.blink = function(slide, index) {
var self = this;
slide.outer.style.display = '';
if (index < this.blinking) {
slide.inner.innerHTML = (index % 2 === 0) ? this.cursor : '';
setTimeout(function() { self.blink(slide, index + 1); }, (500));
} else {
this.type(slide, 0);
}
};
/**
* Types inner text of the givven slide
*/
jsTyper.prototype.type = function(slide, index) {
var self = this;
slide.inner.innerHTML = slide.text.substr(0, index) + this.cursor;
if (index < slide.text.length) {
setTimeout(function() { self.type(slide, index + 1); }, (this.typeDelay));
} else {
slide.inner.innerHTML = slide.text;
this.typing = false;
if (this.playing) {
this.timer = setTimeout(function() {
self.play();
}, (this.slideDelay));
}
}
};
/**
* Adds pause & play functionality
*/
jsTyper.prototype.addEvents = function(el) {
var self = this;
this.resume = function() {
self.playing = true;
self.timer = setTimeout(function() {
self.play();
}, (1000));
};
this.pause = function() {
clearTimeout(self.timer);
self.playing = false;
};
if (el.addEventListener) {
el.addEventListener('mouseover', this.pause, false);
el.addEventListener('mouseout', this.resume, false);
} else {
el.attachEvent('onmouseover', this.pause);
el.attachEvent('onmouseout', this.resume);
}
};
/**
* Removes pause & play functionality
*/
jsTyper.prototype.removeEvents = function(el) {
if (el.removeEventListener) {
el.removeEventListener('mouseover', this.pause, false);
el.removeEventListener('mouseout', this.resume, false);
} else {
el.detachEvent('onmouseover', this.pause);
el.detachEvent('onmouseout', this.resume);
}
};