<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>JavaScript | トーチログ</title>
	<atom:link href="https://tomochilog.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>https://tomochilog.com</link>
	<description>フリーランス夫婦がデザイン、WEB制作、WordPress、日常の気付きを発信中</description>
	<lastBuildDate>Fri, 23 Jul 2021 09:02:09 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.8</generator>

<image>
	<url>https://tomochilog.com/wp-content/uploads/2020/05/cropped-favicon-32x32.png</url>
	<title>JavaScript | トーチログ</title>
	<link>https://tomochilog.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/>
<atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/>
<atom:link rel="hub" href="https://websubhub.com/hub"/>
<atom:link rel="self" href="https://tomochilog.com/category/javascript/feed/"/>
<site xmlns="com-wordpress:feed-additions:1">177821292</site>	<item>
		<title>【jQuery】文字列に自動的にspanタグを設定する方法</title>
		<link>https://tomochilog.com/auto-span/</link>
		
		<dc:creator><![CDATA[TOMO]]></dc:creator>
		<pubDate>Fri, 23 Jul 2021 09:02:07 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://tomochilog.com/?p=721</guid>

					<description><![CDATA[<p>JavaScript（jQuery）を使ったテクニックをご紹介します。 Webサイトで以下のようなテキストを順番に浮き上がらせるようなエフェクトって見かけますよね。 上記のようなエフェクトは、文字ひとつひとつをspanタ [&#8230;]</p>
The post <a href="https://tomochilog.com/auto-span/">【jQuery】文字列に自動的にspanタグを設定する方法</a> first appeared on <a href="https://tomochilog.com">トーチログ</a>.]]></description>
										<content:encoded><![CDATA[<p>JavaScript（jQuery）を使ったテクニックをご紹介します。</p>



<p>Webサイトで以下のようなテキストを順番に浮き上がらせるようなエフェクトって見かけますよね。</p>



<figure class="wp-block-image size-full"><img decoding="async" fetchpriority="high" width="832" height="413" src="https://tomochilog.com/wp-content/uploads/2021/07/text-anime-sample01.gif" alt="" class="wp-image-741"/></figure>



<p>上記のようなエフェクトは、文字ひとつひとつをspanタグを囲うことで簡単に実装できます。</p>


<div class="com-box com-left man"><img src="https://tomochilog.com/wp-content/themes/torchlog/images/com_tomo_icon.png" alt="運営者：TOMO"><div class="chat"><div class="comment"><p>でも、手作業で毎回設定するのって正直面倒…</p></div></div></div>



<p>というわけで、JavaScript（jQuery）を使ってまとめて設定できるようにしてみました。</p>



<p></p>



<h2 class="wp-block-heading">文字列に自動的にspanタグを設定する方法を解説</h2>



<p>まずはサンプルHTMLをご覧ください。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-html" data-lang="HTML"><code>&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;jp&quot;&gt;
&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  // jQuery CDN読み込み
  &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js&quot;&gt;&lt;/script&gt;
  &lt;title&gt;文字列にspanを自動設定するサンプル&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;main&gt;
    &lt;dl class=&quot;text-wrapper&quot;&gt;
      &lt;dt class=&quot;text-span&quot;&gt;About&lt;/dt&gt;
      &lt;dt class=&quot;text-span&quot;&gt;Services&lt;/dt&gt;
      &lt;dt class=&quot;text-span&quot;&gt;Contact Us&lt;/dt&gt;
      &lt;dt class=&quot;text-span&quot;&gt;日本語もいけます&lt;/dt&gt;
    &lt;/dl&gt;
  &lt;/main&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre></div>



<p>サンプルHTML内の『<strong>class=&#8221;text-span&#8221;</strong>』が設定されている文字列（dtタグ）を分解してspanを設定していきます。</p>



<p>例えば<strong>About</strong>という文字列は、以下のようにしたいということですね。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-html" data-lang="HTML"><code>&lt;dt class=&quot;text-span&quot;&gt;
  &lt;span&gt;A&lt;/span&gt;
  &lt;span&gt;b&lt;/span&gt;
  &lt;span&gt;o&lt;/span&gt;
  &lt;span&gt;u&lt;/span&gt;
  &lt;span&gt;t&lt;/span&gt;
&lt;/dt&gt;</code></pre></div>



<p>一文字ずつ手作業で書き換えても良いのですが、新しく要素が追加になった時や文字列の変更、別ページでも同じような記述が発生するとかなり手間に感じます。</p>



<p>そんな時はJavaScript（jQuery）を使って自動設定してしまいましょう！</p>



<p></p>



<p>JavaScriptのサンプルコードはこちら</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-js" data-lang="JavaScript"><code>$(document).ready(function () {

  // 文字列へのspan設定
  $(&quot;.text-span&quot;).each(function() {
    var content = $(this).html();
    var trimText = $.trim(content);
    var newText = &quot;&quot;;

    trimText.split(&quot;&quot;).forEach(function(e) {
      if(e == &#39; &#39;){
        // 空白対策
        newText += &#39;&lt;span&gt;&nbsp;&lt;/span&gt;&#39;;
      } else {
        newText += &#39;&lt;span&gt;&#39; + e + &#39;&lt;/span&gt;&#39;;
      }
    });
    $(this).html(newText);
  });

});
</code></pre></div>



<p>簡単に説明しますと【text-spanクラスが設定されている要素を一文字ずつ取り出して前後にspanタグを付ける】を繰り返す処理です。</p>



<p>これだけ作っておけば、ページ読み込み時に自動実行されるようになって非常に便利です。</p>


<div class="com-box com-left man"><img src="https://tomochilog.com/wp-content/themes/torchlog/images/com_tomo_icon.png" alt="運営者：TOMO"><div class="chat"><div class="comment"><p>別プロジェクトにも流用しやすい！</p></div></div></div>



<p></p>



<h3 class="wp-block-heading">テキストが左から一文字ずつ浮き上がるアニメーションを付けてみる</h3>



<p>span設定ができるとテキストに少し変わったエフェクトを付けられます。今回は冒頭で説明したテキストを順番に浮き上がらせるテキストモーションを実装してみましょう。</p>



<p>まずはCSSを使って、text-spanクラスの要素にshowが設定されたら、文字が浮かび上がるように設定します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-css" data-lang="CSS"><code>.text-span {
  color: #1565C0;
  font-size: 2.0rem;
  letter-spacing: 1.6px;
  margin-bottom: 10px;
  margin-left: 20px;
  overflow: hidden;
  opacity: 0;
}

.text-span.show {
  opacity: 1;  
}

.text-span span {
  display: inline-block;
  transform: translate(0, 105%);
  transition: transform cubic-bezier(0.215, 0.61, 0.355, 1) 0.5s;
}

.text-span.show span {
  transform: translate(0, 0);
}

.text-span.show span:nth-child(2) {
  transition-delay: 0.06s;
}
.text-span.show span:nth-child(3) {
  transition-delay: 0.12s;
}
.text-span.show span:nth-child(4) {
  transition-delay: 0.18s;
}
.text-span.show span:nth-child(5) {
  transition-delay: 0.24s;
}
.text-span.show span:nth-child(6) {
  transition-delay: 0.30s;
}
.text-span.show span:nth-child(7) {
  transition-delay: 0.36s;
}
.text-span.show span:nth-child(8) {
  transition-delay: 0.42s;
}
.text-span.show span:nth-child(9) {
  transition-delay: 0.48s;
}
.text-span.show span:nth-child(10) {
  transition-delay: 0.54s;
}</code></pre></div>



<p><strong>「.text-span.show span:nth-child(x)」</strong>といった記述が複数ありますが、1文字ずつ0.06秒ずつ遅れて動くようにするための工夫となります。今回は10文字まで対応させていますが、使いたい文字数に合わせて調整してみてください。</p>



<p></p>



<p>最後にJavaScript側でshowクラスを設定する処理を追加して完成です！</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-js" data-lang="JavaScript"><code>  setInterval(() =&gt; {
    $(&quot;.text-span&quot;).addClass(&#39;show&#39;);
  }, 400);</code></pre></div>



<p></p>



<p><strong>■完成版</strong></p>



<p class="codepen" data-height="351" data-default-tab="html,result" data-slug-hash="qBmPeoB" data-preview="true" data-user="f-tomoya7" style="height: 351px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;">
  <span>See the Pen <a href="https://codepen.io/f-tomoya7/pen/qBmPeoB">
  code01_text-span01</a> by f-tomoya7 (<a href="https://codepen.io/f-tomoya7">@f-tomoya7</a>)
  on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>



<p>当然ですが、同じアニメーションを別要素でも使いたい時はtext-spanクラスを設定するだけで実装可能です。</p>



<p></p>



<h3 class="wp-block-heading">文字列spanを活用したサンプルコード集</h3>



<p>いくつかspanタグを適用した文字列でエフェクトを作ってみました。</p>



<p class="codepen" data-height="300" data-default-tab="html,result" data-slug-hash="xxdPGQd" data-preview="true" data-user="f-tomoya7" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;">
  <span>See the Pen <a href="https://codepen.io/f-tomoya7/pen/xxdPGQd">
  code01_text-span02</a> by f-tomoya7 (<a href="https://codepen.io/f-tomoya7">@f-tomoya7</a>)
  on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>



<p>文字に強弱を付けることで、キャッチコピーや見出しでインパクトを与えることができます。</p>



<p></p>



<p class="codepen" data-height="464" data-default-tab="html,result" data-slug-hash="oNWojeP" data-preview="true" data-user="f-tomoya7" style="height: 464px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;">
  <span>See the Pen <a href="https://codepen.io/f-tomoya7/pen/oNWojeP">
  code01_text-span03</a> by f-tomoya7 (<a href="https://codepen.io/f-tomoya7">@f-tomoya7</a>)
  on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>



<p>文字単位で動きを付けることができますので、流れるようなモーションを作る時に重宝します。</p>



<p></p>



<h2 class="wp-block-heading">まとめ</h2>



<p>文字列にエフェクトを付ける際に一文字ずつspanタグを設定するのは正直非効率ですし、手作業だとどうしてもミスが生まれたりします。</p>



<p>今回ご紹介したスクリプトは、様々な場面で活用できるかと思いますので、参考にしてみてください。</p>



<p></p>The post <a href="https://tomochilog.com/auto-span/">【jQuery】文字列に自動的にspanタグを設定する方法</a> first appeared on <a href="https://tomochilog.com">トーチログ</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">721</post-id>	</item>
	</channel>
</rss>
