Help us understand the problem. What is going on with this article?

Javascript + CSS で複数行テキストを省略する

実現したいこと

jQueryを使わずに複数行テキスト末尾に「...」をつけたい。
よくある処理なのに忘れがちなので備忘録。

TL;DR

https://playcode.io/484998
スクリーンショット 2020-01-18 15.22.24.png

参考

クラスメソッドさんの記事を土台にVanilla JSに書き換えました。
クラスメソッドさん、いつもありがとうございます。
https://dev.classmethod.jp/ria/string-replace-css-and-jquery/

コード

index.html
<div class="wrap">
  <p class="ellipsis">
    あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、
    うつくしい森で飾られたモリーオ市、
    郊外のぎらぎらひかる草の波。
  </p>
</div>
ellipsis.css
.ellipsis {
  width: 296px;
  height: 48px;
  font-size: 16px;
  overflow: hidden;
}
ellipsis.js
const target = document.getElementsByClassName("ellipsis")[0];
let text = target.innerHTML;

const clone = target.cloneNode(true);
clone.style.visibility  = "hidden";
clone.style.position    = "absolute";
clone.style.overflow    = "visible";
clone.style.width       = `${target.clientWidth}px`;
clone.style.height      = "auto";

target.insertAdjacentElement("afterend", clone);

while((text.length > 0) && (clone.clientHeight > target.clientHeight)) {
  text = text.substr(0, text.length - 1);
  clone.innerHTML = `${text}...`;
}

target.innerHTML = clone.innerHTML;
clone.parentNode.removeChild(clone);

解説

▶︎何もしていない状態

ellipsis.css
.ellipsis {
  width: 296px;
  font-size: 16px;
}

スクリーンショット 2020-01-18 15.34.42.png

▶︎高さをheight: 48px;で指定し、要素からはみ出したテキストをoverflow: hidden;で隠す

ellipsis.css
.ellipsis {
  width: 296px;
  height: 48px;
  font-size: 16px;
  overflow: hidden;
}

スクリーンショット 2020-01-18 15.51.32.png

▶︎JSの処理を追加
テキストの<p>要素を複製して、直後に追加

ellipsis.js
const target = document.getElementsByClassName("ellipsis")[0];
let text = target.innerHTML;

const clone = target.cloneNode(true);
clone.style.visibility  = "hidden"; // display: "none"だとclientHeightが取得できない
clone.style.position    = "absolute";
clone.style.overflow    = "visible";
clone.style.width       = `${target.clientWidth}px`;
clone.style.height      = "auto";

target.insertAdjacentElement("afterend", clone);

▶︎複製した要素を追加した状態
スクリーンショット 2020-01-18 15.54.48.png

▶︎複製した要素のheightが元の要素のheightを下回るまで文字を減らしていく

ellipsis.js
while((text.length > 0) && (clone.clientHeight > target.clientHeight)) {
  text = text.substr(0, text.length - 1);
  clone.innerHTML = `${text}...`;
}

▶︎複製した要素のテキストを元の要素に代入、複製要素は不要なので削除

ellipsis.js
target.innerHTML = clone.innerHTML;
clone.parentNode.removeChild(clone);

:100::v::100::v::100::v::100::v::100:
スクリーンショット 2020-01-18 15.22.24.png

複製要素もちゃんと削除されてます:ok_hand:
スクリーンショット 2020-01-18 16.13.47.png

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした