Skip to content

Commit 46aafa6

Browse files
committed
Changing Text
1 parent 21f9928 commit 46aafa6

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

assets/js/custom.js

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,58 +19,63 @@
1919

2020
// animate function on head
2121
(function($) {
22-
var words = ['passionate','focused','enthusiastic'],
23-
currentStep = 0,
24-
textEl = document.querySelector('#highlights'),
25-
oldWord = '';
26-
27-
setTimeout(changeWord, 1500);
28-
29-
function changeWord() {
30-
oldWord = textEl.innerHTML;
31-
// check if there is a word atm or not
32-
if (oldWord.length < 1) {
33-
if (currentStep !== words.length -1) {
34-
currentStep ++;
35-
} else {
36-
currentStep = 0;
22+
var textHolder = document.querySelector("#highlights");
23+
var newWord;
24+
var currentWord = "";
25+
26+
function changingText() {
27+
this.words = ['passionate','focused','enthusiastic'];
28+
this.step = 0;
29+
}
30+
31+
var ChangingText = new changingText();
32+
33+
34+
ChangingText.addWord = function() {
35+
if(currentWord.length < 1) {
36+
newWord = this.words[this.step];
37+
}
38+
39+
var currentLength = currentWord.length;
40+
41+
if(currentLength == newWord.length) {
42+
ChangingText.deleteWord();
43+
return;
3744
}
38-
addNextWord();
39-
} else {
40-
setTimeout(deleteWord, 1200);
45+
46+
// add new word by single character
47+
currentWord = newWord.substring(0, currentLength + 1);
48+
textHolder.innerHTML = currentWord;
49+
setTimeout(ChangingText.addWord, 300);
4150
}
42-
};
4351

44-
function deleteWord() {
45-
var WordLength = oldWord.length,
46-
currentWord = textEl.innerHTML,
47-
currentLength = currentWord.length;
52+
ChangingText.changeWord = function() {
53+
++this.step; // increment step to get next word
54+
if(this.step == this.words.length) {
55+
this.step = 0;
56+
}
4857

49-
// The word is deleted so, start adding in the new one
50-
if (currentLength < 1) {
51-
changeWord();
58+
ChangingText.addWord();
5259
return;
5360
}
54-
// Remove a charachter
55-
textEl.innerHTML = currentWord.substring(0, currentLength - 1);
56-
setTimeout(deleteWord, 300);
57-
}
5861

59-
function addNextWord() {
60-
var currentWord = textEl.innerHTML,
61-
currentLength = currentWord.length,
62-
nextWord = words[currentStep],
63-
nextWordLength = nextWord.length;
64-
65-
if (currentLength === nextWordLength) {
66-
changeWord();
62+
ChangingText.deleteWord = function() {
63+
if(currentWord.length < 1) {
64+
ChangingText.changeWord();
6765
return;
6866
}
6967

70-
// add a charachter
71-
textEl.innerHTML = nextWord.substring(0, currentLength + 1);
72-
setTimeout(addNextWord, 300);
73-
}
68+
var currentLength = currentWord.length;
69+
70+
// remove word by single character
71+
currentWord = currentWord.substring(0, currentLength - 1);
72+
textHolder.innerHTML = currentWord;
73+
setTimeout(ChangingText.deleteWord, 300);
74+
}
75+
76+
// start with new word
77+
document.onready = ChangingText.addWord();
78+
7479
})(jQuery);
7580

7681
// Portfolio

0 commit comments

Comments
 (0)