1+
2+ /*@animatio : https://github.com/ahkamboh/animatio, creator:@ahkamboh(https://alihamzakamboh.com) , Usage example: const animatio = new GlideText(["Capture", "Animation"], 1, "vertical");*/
3+ class GlideText {
4+ constructor ( textArray , speed = 1 , direction = "vertical" ) {
5+ this . textArray = textArray ;
6+ this . speed = speed ;
7+ this . direction = direction ;
8+ this . currentIndex = 0 ;
9+ this . textElements = document . querySelectorAll ( '.GlideText' ) ;
10+ this . loadAnimeJS ( ) . then ( ( ) => this . startTextAnimation ( ) ) ;
11+ }
12+
13+ loadAnimeJS ( ) {
14+ return new Promise ( ( resolve , reject ) => {
15+ if ( typeof anime !== 'undefined' ) {
16+ resolve ( ) ;
17+ } else {
18+ const script = document . createElement ( 'script' ) ;
19+ script . src = 'https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js' ;
20+ script . onload = resolve ;
21+ script . onerror = reject ;
22+ document . head . appendChild ( script ) ;
23+ }
24+ } ) ;
25+ }
26+
27+ animateText ( element ) {
28+ if ( element ) {
29+ let translateIn , translateOut ;
30+
31+ switch ( this . direction ) {
32+ case "top" :
33+ translateIn = [ 20 , 0 ] ;
34+ translateOut = - 20 ;
35+ break ;
36+ case "bottom" :
37+ translateIn = [ - 20 , 0 ] ;
38+ translateOut = 20 ;
39+ break ;
40+ case "left" :
41+ translateIn = [ 20 , 0 ] ;
42+ translateOut = - 20 ;
43+ break ;
44+ case "right" :
45+ translateIn = [ - 20 , 0 ] ;
46+ translateOut = 20 ;
47+ break ;
48+ case "horizontal" :
49+ translateIn = [ 20 , 0 ] ;
50+ translateOut = - 20 ;
51+ break ;
52+ case "vertical" :
53+ default :
54+ translateIn = [ 20 , 0 ] ;
55+ translateOut = - 20 ;
56+ break ;
57+ }
58+
59+ anime . timeline ( { loop : false } )
60+ . add ( {
61+ targets : element ,
62+ opacity : [ 0 , 1 ] ,
63+ translateY : this . direction === "top" || this . direction === "bottom" || this . direction === "vertical" ? translateIn : 0 ,
64+ translateX : this . direction === "left" || this . direction === "right" || this . direction === "horizontal" ? translateIn : 0 ,
65+ easing : "easeOutExpo" ,
66+ duration : 1000 / this . speed
67+ } )
68+ . add ( {
69+ targets : element ,
70+ opacity : 0 ,
71+ translateY : this . direction === "top" || this . direction === "bottom" || this . direction === "vertical" ? translateOut : 0 ,
72+ translateX : this . direction === "left" || this . direction === "right" || this . direction === "horizontal" ? translateOut : 0 ,
73+ easing : "easeInExpo" ,
74+ duration : 1000 / this . speed ,
75+ } ) ;
76+
77+ this . currentIndex = ( this . currentIndex + 1 ) % this . textArray . length ;
78+ }
79+ }
80+
81+ startTextAnimation ( ) {
82+ this . textElements . forEach ( element => this . animateText ( element ) ) ;
83+ setInterval ( ( ) => {
84+ this . textElements . forEach ( element => this . animateText ( element ) ) ;
85+ } , 2000 / this . speed ) ;
86+ }
87+ }
0 commit comments