0
我有字符串文本,我想根據屏幕寬度顯示我隱藏了多少個字。如何根據元素寬度截斷文本,角?
這是我到目前爲止有:
app.filter('words', function() {
return function (input, words) {
if (isNaN(words)) return input;
if (words <= 0) return '';
if (input) {
var inputWords = input.split(/\s+/);
if (inputWords.length > words) {
var theLength = inputWords.length - words;
input = inputWords.slice(0, words).join(' ') + ' + ' + theLength;
}
}
return input;
};
});
這個濾鏡單詞的固定數。如果words
= 5表示我們只能看到5個單詞,其他將被隱藏。
但我正在尋找的方式,使words
號碼動態根據元素的寬度。例如對於<div>
寬度200像素我顯示12個單詞,(也許更多也許更少),爲40px - 或零字(如果字太長)或一個字,
我想我需要混合一些指令,應該元素寬度並計算words
數字。
這是一個演示:
非常感謝您的幫助,
謝謝你隊友,偉大的工作! – snaggs
順便說一句,你平均字長'scope.width/15',這是有些情況下,這種邏輯錯誤 – snaggs
我只是從一頂帽子拉出該公式。無論如何,我想你會想出自己的想法。 – Jerrad