前回の質問で<br>はspanで囲わないというのは解決できたのですが
同じクラスが複数有る場合だとeachはcontentsで使っているためすべてのtext_animateクラスから探してきて
とってくるため複数のクラスの配列が処理一つのクラスに同時に出力されるためうまく動きません
プラグイン化、もしくはdate属性を使えばこの問題は解決するかもしれませんが何か良い
解決はないでしょうか?
html
1<p class="text_animate">te<br>st</p> 2<p class="text_animate">te<br>st</p> 3<p class="text_animate">te<br>st</p> 4<p class="text_animate">te<br>st</p> 5<p class="text_animate">te<br>st</p>
scss
1.text_animate{ 2 span{ 3 transition: 1s; 4 opacity: 0; 5 &.text_animate_on{ 6 transition: 1s; 7 opacity: 1; 8 9 } 10 } 11 &.text_animate_rotate span{ 12 transform: rotate(-45deg); 13 display: inline-block; 14 &.text_animate_rotate_on{ 15 transform: rotate(0deg); 16 } 17 } 18 &.text_animate_size span{ 19 font-size: 22px; 20 &.text_animate_size_on{ 21 font-size: 10px; 22 } 23 } 24}
javascript
1$(function () { 2 // 子ノード3つそれぞれに対して処理 3 $(".text_animate").contents().each(function (_, node) { 4 // 子ノードを一旦DOMツリーから除去 5 // 変数`node`はjQueryオブジェクトではないので`node.remove();`とは書けません。 6 node.parentNode.removeChild(node); 7 8 // ノードの種類によって条件分岐 9 switch (node.nodeType) { 10 // テキストノードなら質問文にある処理を適用 11 case Node.TEXT_NODE: 12 // テキストノードの文字列を取得 13 var text_split = node.textContent.split(""); 14 15 function animate() { 16 text_split.forEach(function (val) { 17 $(".text_animate").append("<span>" + val + "</span>"); 18 }); 19 } 20 animate(); 21 break; 22 // テキストノード以外(HTML要素)ならappendし直す 23 default: 24 $(".text_animate").append(node); 25 } 26 }); 27 28 // 以下、変更点なし 29 $(window).load(function () { 30 $(".text_animate span").each(function (index) { 31 $(this).css({ 32 transitionDelay: index * 0.6 + "s" 33 }).addClass("text_animate_on"); 34 }); 35 }); 36});
追記:こちらの方法で行けるみたいですが
できるみたいですが処理の内容がいまいちわかりません
javascript
1$(".text_animate").children().andSelf().contents().each(function () { 2 if (this.nodeType == 3) { 3 $(this).replaceWith($(this).text().replace(/(\S)/g, '<span>$1</span>')); 4 } 5 });
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/17 00:26
2018/08/17 14:56
2018/08/19 13:33