|
19 | 19 |
|
20 | 20 | // animate function on head
|
21 | 21 | (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; |
37 | 44 | }
|
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); |
41 | 50 | }
|
42 |
| - }; |
43 | 51 |
|
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 | + } |
48 | 57 |
|
49 |
| - // The word is deleted so, start adding in the new one |
50 |
| - if (currentLength < 1) { |
51 |
| - changeWord(); |
| 58 | + ChangingText.addWord(); |
52 | 59 | return;
|
53 | 60 | }
|
54 |
| - // Remove a charachter |
55 |
| - textEl.innerHTML = currentWord.substring(0, currentLength - 1); |
56 |
| - setTimeout(deleteWord, 300); |
57 |
| - } |
58 | 61 |
|
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(); |
67 | 65 | return;
|
68 | 66 | }
|
69 | 67 |
|
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 | + |
74 | 79 | })(jQuery);
|
75 | 80 |
|
76 | 81 | // Portfolio
|
|
0 commit comments