作業中のクロスブラウザー ソリューション
この問題は何年もの間、私たち全員を悩ませてきました。
すべての場合に役立つように、CSS のみのアプローチと、css の警告が問題になる場合に備えて jQuery アプローチを用意しました。
これは私が思いついたCSS のみのソリューションで、すべての状況で機能しますが、いくつかの小さな注意事項があります。
基本はシンプルで、スパンのオーバーフローを隠し、Eugene Xa が提案する行の高さに基づいて最大の高さを設定します。
次に、含まれている div の後に、省略記号を適切に配置する疑似クラスがあります。
注意事項
このソリューションは、必要があるかどうかに関係なく、常に省略記号を配置します。
最後の行が終わりの文で終わっている場合、4 つのドットで終わる....
正当化されたテキストの配置に満足する必要があります。
省略記号はテキストの右側にあるため、見にくくなります。
コード + スニペット
jsfiddle
.text {
position: relative;
font-size: 14px;
color: black;
width: 250px; /* Could be anything you like. */
}
.text-concat {
position: relative;
display: inline-block;
word-wrap: break-word;
overflow: hidden;
max-height: 3.6em; /* (Number of lines you want visible) * (line-height) */
line-height: 1.2em;
text-align:justify;
}
.text.ellipsis::after {
content: "...";
position: absolute;
right: -12px;
bottom: 4px;
}
/* Right and bottom for the psudo class are px based on various factors, font-size etc... Tweak for your own needs. */
<div class="text ellipsis">
<span class="text-concat">
Lorem ipsum dolor sit amet, nibh eleifend cu his, porro fugit mandamus no mea. Sit tale facete voluptatum ea, ad sumo altera scripta per, eius ullum feugait id duo. At nominavi pericula persecuti ius, sea at sonet tincidunt, cu posse facilisis eos. Aliquid philosophia contentiones id eos, per cu atqui option disputationi, no vis nobis vidisse. Eu has mentitum conclusionemque, primis deterruisset est in.
Virtute feugait ei vim. Commune honestatis accommodare pri ex. Ut est civibus accusam, pro principes conceptam ei, et duo case veniam. Partiendo concludaturque at duo. Ei eirmod verear consequuntur pri. Esse malis facilisis ex vix, cu hinc suavitate scriptorem pri.
</span>
</div>
jQuery アプローチ
私の意見では、これが最善の解決策ですが、誰もが JS を使用できるわけではありません。基本的に、jQuery は任意の .text 要素をチェックし、あらかじめ設定された最大変数よりも多くの文字がある場合、残りを切り捨てて省略記号を追加します。
このアプローチに警告はありませんが、このコード例は基本的なアイデアを示すことのみを目的としています。次の 2 つの理由から、改善せずに本番環境でこれを使用することはありません。
1) .text 要素の内部 html を書き換えます。必要かどうか。2) 内部の html にネストされた要素がないことを確認するテストは行わないため、作成者が .text を正しく使用することに大きく依存しています。
編集済み
キャッチ@markzzzをありがとう
コードスニペット
jsfiddle
setTimeout(function()
{
var max = 200;
var tot, str;
$('.text').each(function() {
str = String($(this).html());
tot = str.length;
str = (tot <= max)
? str
: str.substring(0,(max + 1))+"...";
$(this).html(str);
});
},500); // Delayed for example only.
.text {
position: relative;
font-size: 14px;
color: black;
font-family: sans-serif;
width: 250px; /* Could be anything you like. */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="text">
Old men tend to forget what thought was like in their youth; they forget the quickness of the mental jump, the daring of the youthful intuition, the agility of the fresh insight. They become accustomed to the more plodding varieties of reason, and because this is more than made up by the accumulation of experience, old men think themselves wiser than the young.
</p>
<p class="text">
Old men tend to forget what thought was like in their youth;
</p>
<!-- Working Cross-browser Solution
This is a jQuery approach to limiting a body of text to n words, and end with an ellipsis -->