w2utils.getStrWidth
更新日: 2018-09-07
getStrWidth(str, styles)
文字列の幅をpxで返します。 戻り値はstringです。
- str: HTML要素
- 幅を計算する文字列。
- styles: string
- 文字列に適用するスタイル。
説明
文字列の幅を計算して返します。
以下のように指定することができます。
var width = w2utils.getStrWidth('Some text', 'font-size: 40px; font-weight: bold;');
console.log(width);デモ
"デモ用文字列"の文字幅をconsole.logに出力します。getStrWidthとgetSizeでほぼ同じ結果になります。
JavaScript
var width = w2utils.getStrWidth('デモ用文字列', 'font-size: 40px; font-weight: bold;');
console.log('.getStrWidth : ' + width);
width = w2utils.getSize($('#demo'), 'width');
console.log('.getSize : ' + width);CSS
#demo {
  font-size: 40px;
  font-weight: bold;
}HTML
<span id="demo">デモ用文字列</span>© 2017.  .
.
Code licensed under theMIT License. Documentation licensed underCC BY 3.0.
このコンテンツはvitmalinaによるw2ui.utilsドキュメントを翻訳/改変したものです。