@@ -2,83 +2,134 @@ document.addEventListener("contextmenu", (event) => {
2
2
event . preventDefault ( ) ;
3
3
} ) ;
4
4
5
- const texts = [ "I'm Rifzkhy" , "I'm Programmer" , "I'm Desainer " ] ;
5
+ const texts = [ "I'm Rifzkhy" , "I'm Programmer" , "I'm Designer " ] ;
6
6
7
- const typingElement = document . querySelector ( ".typing" ) ;
8
- let textIndex = 0 ;
7
+ const typingElements = document . querySelectorAll ( ".typing" ) ;
9
8
10
- function type ( ) {
9
+ function type ( element , text ) {
11
10
let i = 0 ;
12
- const text = texts [ textIndex ] ;
11
+
13
12
const typingInterval = setInterval ( ( ) => {
14
13
if ( i < text . length ) {
15
- typingElement . textContent += text . charAt ( i ) ;
14
+ element . textContent += text . charAt ( i ) ;
16
15
i ++ ;
17
16
} else {
18
17
clearInterval ( typingInterval ) ;
19
- setTimeout ( erase , 1000 ) ; // Wait for a moment before erasing
18
+ setTimeout ( ( ) => erase ( element ) , 1000 ) ;
20
19
}
21
- } , 200 ) ; // Adjust the typing speed by changing the interval (e.g., 50 for faster typing).
20
+ } , 200 ) ;
22
21
}
23
22
24
- function erase ( ) {
23
+ function erase ( element ) {
24
+ const text = element . textContent ;
25
+
25
26
const typingInterval = setInterval ( ( ) => {
26
- if ( typingElement . textContent . length > 0 ) {
27
- typingElement . textContent = typingElement . textContent . slice ( 0 , - 1 ) ;
27
+ if ( element . textContent . length > 0 ) {
28
+ element . textContent = element . textContent . slice ( 0 , - 1 ) ;
28
29
} else {
29
30
clearInterval ( typingInterval ) ;
30
- textIndex = ( textIndex + 1 ) % texts . length ; // Move to the next text
31
- setTimeout ( type , 500 ) ; // Wait for a moment before typing the next text
31
+ setTimeout ( ( ) => {
32
+ textIndex = ( textIndex + 1 ) % texts . length ;
33
+ type ( element , texts [ textIndex ] ) ;
34
+ } , 500 ) ;
32
35
}
33
- } , 50 ) ; // Adjust the erasing speed if needed
36
+ } , 50 ) ;
34
37
}
35
38
36
- window . addEventListener ( "load" , ( ) => {
37
- $ ( window ) . on ( "load" , function ( ) {
38
- $ ( "html, body" ) . animate ( { scrollTop : 0 } ) ;
39
- $ ( ".loader" ) . fadeOut ( 1000 ) ;
40
- $ ( ".content" ) . fadeIn ( 1000 ) ;
41
- setTimeout ( type , 1000 ) ;
39
+ function checkInView ( ) {
40
+ typingElements . forEach ( ( element ) => {
41
+ const rect = element . getBoundingClientRect ( ) ;
42
+ if ( rect . top >= 0 && rect . bottom <= window . innerHeight ) {
43
+ // Element is in view, start typing effect
44
+ if ( element . textContent === "" ) {
45
+ textIndex = ( textIndex + 1 ) % texts . length ;
46
+ type ( element , texts [ textIndex ] ) ;
47
+ }
48
+ } else {
49
+ // Element is out of view, reset effect
50
+ element . textContent = "" ;
51
+ }
42
52
} ) ;
43
- } ) ;
53
+ }
54
+
55
+ // Check if elements are in view when the page loads
56
+ checkInView ( ) ;
57
+
58
+ // Check if elements are in view when the user scrolls
59
+ window . addEventListener ( "scroll" , checkInView ) ;
44
60
61
+ //! Scroll effect
45
62
const scrollElements = document . querySelectorAll ( ".js-scroll" ) ;
46
63
47
- const elementInView = ( el , dividend = 1 ) => {
64
+ function elementInView ( el , dividend = 1 ) {
48
65
const elementTop = el . getBoundingClientRect ( ) . top ;
49
-
50
66
return (
51
67
elementTop <=
52
68
( window . innerHeight || document . documentElement . clientHeight ) / dividend
53
69
) ;
54
- } ;
70
+ }
55
71
56
- const elementOutofView = ( el ) => {
72
+ function elementOutofView ( el ) {
57
73
const elementTop = el . getBoundingClientRect ( ) . top ;
58
-
59
74
return (
60
75
elementTop > ( window . innerHeight || document . documentElement . clientHeight )
61
76
) ;
62
- } ;
77
+ }
63
78
64
- const displayScrollElement = ( element ) => {
79
+ function displayScrollElement ( element ) {
65
80
element . classList . add ( "scrolled" ) ;
66
- } ;
81
+ }
67
82
68
- const hideScrollElement = ( element ) => {
83
+ function hideScrollElement ( element ) {
69
84
element . classList . remove ( "scrolled" ) ;
70
- } ;
85
+ }
71
86
72
- const handleScrollAnimation = ( ) => {
87
+ function handleScrollAnimation ( ) {
73
88
scrollElements . forEach ( ( el ) => {
74
89
if ( elementInView ( el , 1.25 ) ) {
75
90
displayScrollElement ( el ) ;
76
91
} else if ( elementOutofView ( el ) ) {
77
92
hideScrollElement ( el ) ;
78
93
}
79
94
} ) ;
80
- } ;
95
+ }
81
96
82
- window . addEventListener ( "scroll" , ( ) => {
83
- handleScrollAnimation ( ) ;
84
- } ) ;
97
+ // Check if elements are in view when the page loads
98
+ handleScrollAnimation ( ) ;
99
+
100
+ // Check if elements are in view when the user scrolls
101
+ window . addEventListener ( "scroll" , handleScrollAnimation ) ;
102
+
103
+ //! Writing effect
104
+ const elements = document . querySelectorAll ( ".write" ) ;
105
+
106
+ function type ( element ) {
107
+ const text = element . textContent ;
108
+ element . textContent = "" ;
109
+
110
+ let i = 0 ;
111
+
112
+ function typeCharacter ( ) {
113
+ if ( i < text . length ) {
114
+ element . textContent += text . charAt ( i ) ;
115
+ i ++ ;
116
+ setTimeout ( typeCharacter , 200 ) ;
117
+ }
118
+ }
119
+
120
+ function checkInView ( ) {
121
+ const rect = element . getBoundingClientRect ( ) ;
122
+ if ( rect . top >= 0 && rect . bottom <= window . innerHeight ) {
123
+ // Element is in view, start typing effect
124
+ typeCharacter ( ) ;
125
+ }
126
+ }
127
+
128
+ // Check if element is in view when the page loads
129
+ checkInView ( ) ;
130
+
131
+ // Check if element is in view when the user scrolls
132
+ window . addEventListener ( "scroll" , checkInView ) ;
133
+ }
134
+
135
+ elements . forEach ( type ) ;
0 commit comments