Javascript 如何根据元素宽度、角度截断文本?
我有字符串文本,我想根据屏幕宽度显示我隐藏了多少单词 这就是我到目前为止所做的:Javascript 如何根据元素宽度、角度截断文本?,javascript,angularjs,Javascript,Angularjs,我有字符串文本,我想根据屏幕宽度显示我隐藏了多少单词 这就是我到目前为止所做的: app.filter('words', function () { return function (input, words) { if (isNaN(words)) return input; if (words <= 0) return ''; if (input) { var in
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;
};
});
app.filter('words',function(){
返回函数(输入,字){
if(isNaN(words))返回输入;
如果(字){
var theLength=inputWords.length-单词;
input=inputWords.slice(0,words.join)(“”)+'+'+长度;
}
}
返回输入;
};
});
此筛选器适用于固定字数。如果words
=5意味着我们只能看到5个单词,其他单词将被隐藏
但我正在寻找一种方法,使单词
数字根据元素宽度动态变化。例如,对于宽度为200 px的
我显示了12个单词(可能更多,可能更少),对于40 px-或零个单词(如果单词太长)或一个单词
我想我需要混合一些指令,这些指令应该采用元素宽度并计算单词
数字
这是一个演示:
非常感谢您的帮助,这是我制定的指令:
app.directive('wordCount', function($compile){
return{
restrict: 'E',
replace: true,
scope: {
width: '=',
text: '='
},
template: '<div style ="border-style: solid; width:{{width}}px"><span>{{text}}</span></div>',
link: function(scope, element, attrs){
scope.$watch('width', function(value){
if(isNaN(scope.width) || scope.width < 0)
return;
var numWords = Math.floor(scope.width / 15);
var inputWords = scope.text.split(/\s+/);
var theLength = inputWords.length - numWords;
console.log('Element width: ' + scope.width);
console.log("# of words: " + inputWords.length);
console.log("# of words to show: " + numWords);
element[0].innerHTML = inputWords.slice(0, numWords).join(' ') + ' + ' + theLength;
});
}
};
});
其中,
是希望div的宽度,
是希望显示的文本。我使用了一个简单的width/15
公式来确定要显示的字数。你可能想想出一些更复杂的东西
这是一个演示它实际运行的指令。这是我突然提出的一个指令:
app.directive('wordCount', function($compile){
return{
restrict: 'E',
replace: true,
scope: {
width: '=',
text: '='
},
template: '<div style ="border-style: solid; width:{{width}}px"><span>{{text}}</span></div>',
link: function(scope, element, attrs){
scope.$watch('width', function(value){
if(isNaN(scope.width) || scope.width < 0)
return;
var numWords = Math.floor(scope.width / 15);
var inputWords = scope.text.split(/\s+/);
var theLength = inputWords.length - numWords;
console.log('Element width: ' + scope.width);
console.log("# of words: " + inputWords.length);
console.log("# of words to show: " + numWords);
element[0].innerHTML = inputWords.slice(0, numWords).join(' ') + ' + ' + theLength;
});
}
};
});
其中,
是希望div的宽度,
是希望显示的文本。我使用了一个简单的width/15
公式来确定要显示的字数。你可能想想出一些更复杂的东西
这是一个演示它实际运行的指令。这是我突然提出的一个指令:
app.directive('wordCount', function($compile){
return{
restrict: 'E',
replace: true,
scope: {
width: '=',
text: '='
},
template: '<div style ="border-style: solid; width:{{width}}px"><span>{{text}}</span></div>',
link: function(scope, element, attrs){
scope.$watch('width', function(value){
if(isNaN(scope.width) || scope.width < 0)
return;
var numWords = Math.floor(scope.width / 15);
var inputWords = scope.text.split(/\s+/);
var theLength = inputWords.length - numWords;
console.log('Element width: ' + scope.width);
console.log("# of words: " + inputWords.length);
console.log("# of words to show: " + numWords);
element[0].innerHTML = inputWords.slice(0, numWords).join(' ') + ' + ' + theLength;
});
}
};
});
其中,
是希望div的宽度,
是希望显示的文本。我使用了一个简单的width/15
公式来确定要显示的字数。你可能想想出一些更复杂的东西
这是一个演示它实际运行的指令。这是我突然提出的一个指令:
app.directive('wordCount', function($compile){
return{
restrict: 'E',
replace: true,
scope: {
width: '=',
text: '='
},
template: '<div style ="border-style: solid; width:{{width}}px"><span>{{text}}</span></div>',
link: function(scope, element, attrs){
scope.$watch('width', function(value){
if(isNaN(scope.width) || scope.width < 0)
return;
var numWords = Math.floor(scope.width / 15);
var inputWords = scope.text.split(/\s+/);
var theLength = inputWords.length - numWords;
console.log('Element width: ' + scope.width);
console.log("# of words: " + inputWords.length);
console.log("# of words to show: " + numWords);
element[0].innerHTML = inputWords.slice(0, numWords).join(' ') + ' + ' + theLength;
});
}
};
});
其中,
是希望div的宽度,
是希望显示的文本。我使用了一个简单的width/15
公式来确定要显示的字数。你可能想想出一些更复杂的东西
这是一个实际应用的例子。顺便说一句,你取了平均字长
scope.width/15
,这是一些情况下的错误逻辑。我刚刚从帽子里拿出了这个公式。顺便说一句,你取了平均字长scope.width/15
,这是因为有些情况下,这种逻辑是错误的。我刚刚从帽子里拿出了这个公式。顺便说一句,你取了平均字长scope.width/15
,这是因为有些情况下,这种逻辑是错误的。我刚刚从帽子里拿出了这个公式。顺便说一句,你取了平均字长scope.width/15
,这是因为有些情况下,这种逻辑是错误的。我刚刚从帽子里拿出了这个公式。我想你无论如何都会想出自己的主意的。