jQueryのちょっとしたプラグインを作成しています。
テキストを一文字ずつトランジションさせるエフェクトですが、プラグインのコールバックがうまく動きません。
サンプルとして、プラグインエフェクト後にコールバックで一文字ずつテキストカラーを変更しようとしたのですが、コールバック内で$(this)が正常に取得できていないのか、.css()がうまく適用できていない感じです。
何かいい記述の仕方はありますでしょうか。
お知恵をお借りできると嬉しいです。
該当のソースコード
html
1<p class="ex1">TEXTYLE</p> 2<p class="ex2">TEXTYLE </p> 3<p class="desc">- Simple Text Effect -</p>
css
1@import url("https://fonts.googleapis.com/css?family=Heebo:900"); 2html, body { 3 width: 100%; 4 height: 100%; 5 margin: 0; 6 padding: .2rem 0 0 0; 7} 8 9body { 10 display: flex; 11 flex-direction: column; 12 align-items: center; 13 justify-content: center; 14 font-family: 'Heebo', sans-serif; 15 background: #f4f4f4; 16} 17 18p { 19 margin: 0; 20 padding: 0; 21 font-size: 7rem; 22 line-height: 1; 23 letter-spacing: .3rem; 24 color: #222; 25} 26p.desc { 27 padding: 1rem; 28 font-size: 1rem; 29 letter-spacing: .2rem; 30 opacity: 0; 31 transition: 1.5s 2s; 32} 33 34@media screen and (max-width: 450px) { 35 p { 36 font-size: 3rem; 37 } 38 p.desc { 39 font-size: .5rem; 40 } 41} 42.ex1 { 43 opacity: 0; 44 perspective: 300px; 45} 46.ex1 span { 47 display: inline-block; 48 transform: rotateY(-90deg); 49 opacity: 0; 50} 51 52.ex2 { 53 opacity: 0; 54 perspective: 300px; 55} 56.ex2 span { 57 display: inline-block; 58 transform: rotateY(-90deg); 59 opacity: 0; 60}
js
1//////plugin code from here////// 2(function($){ 3 $.fn.textyle = function(options){ 4 var target = this; 5 var targetTxt = target.contents(); 6 var defaults = { 7 duration : 1500, 8 delay : 150, 9 easing : 'cubic-bezier(0.95, 0.05, 0.795, 0.035)', 10 callback : null 11 }; 12 var setting = $.extend(defaults, options); 13 //split txt & wrap txt by span 14 targetTxt.each(function(){ 15 var target = $(this); 16 if(this.nodeType === 3){ 17 mkspn(target); 18 } 19 }); 20 function mkspn(elem){ 21 elem.replaceWith(elem.text().replace(/(\S)/g,'<span>$1</span>')); 22 } 23 //txt animation 24 this.each(function(i){ 25 var target = $(this); 26 var child = target.children('span'); 27 target.css('opacity',1); 28 child.each(function(i){ 29 $(this).delay(setting.delay*i) 30 .queue(function(next) { 31 $(this).css({ 32 transform : 'rotateY(0deg)', 33 opacity : 1, 34 transitionDuration : setting.duration + 'ms', 35 transitionTimingFunction : setting.easing 36 }) 37 next(); 38 }); 39 if(setting.callback){ 40 setting.callback(); 41 } 42 }); 43 }); 44 return this; 45 }; 46}( jQuery )); 47//////plugin code to here////// 48 49 50$(window).on('load',function(){ 51//////code to call////// 52 $('.ex1').textyle(); 53 $('.ex2').textyle({ 54 duration : 700, 55 delay : 200, 56 easing : 'swing', 57 callback : function(){ 58 $(this).css({ //この.css()メソッドが適用されず、文字色が変わらない 59 color : 'coral', 60 transition : '1s', 61 }); 62 $('.desc').css('opacity',1); 63 } 64 }); 65});
###やりたいこと
jQueryプラグインのコールバックを実装したい。
①メイン処理内で要素内テキストを一文字ずつ分割してspanで囲む
②エフェクトを一文字ずつに適用
③コールバックで$(this)を用い、分割したテキスト毎に.css()を適用させたい。
###現状
上記③がうまくいかない。
コールバック内で$(this)の値が正常に取得出来ていないのか、.css()メソッドが適用されず文字色が変わらない。
###Codepen
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/06 18:40
2019/01/06 23:54
2019/01/07 10:31