admin管理员组文章数量:1336281
I am having a div which will be having dynamic width, based on the width I have to restrict the content within that div,I have created a filter which will cut the text and append "..." to the end of the string, but the issue is how we can assign the limit based on the dynamic width size
my code is as given below
JSFiddle
html
<div ng-app="app" ng-controller="MainCtrl">
<div class="test" ng-style="myStyle">{{value | splice:12:' ...'}}</div>
</div>
script
angular.module('app', [])
.controller('MainCtrl', function ($scope) {
$scope.myStyle={};
$scope.myStyle.width = "150px";
$scope.value= "My value is anythinggggggggggggggggg";
})
.filter('splice', function () {
return function (value, max, tail) {
if (!value) return '';
max = parseInt(max, 10);
if (!max) return value;
if (value.length <= max) return value;
value = value.substr(0, max);
return value + (tail || ' …');
};
});
I am having a div which will be having dynamic width, based on the width I have to restrict the content within that div,I have created a filter which will cut the text and append "..." to the end of the string, but the issue is how we can assign the limit based on the dynamic width size
my code is as given below
JSFiddle
html
<div ng-app="app" ng-controller="MainCtrl">
<div class="test" ng-style="myStyle">{{value | splice:12:' ...'}}</div>
</div>
script
angular.module('app', [])
.controller('MainCtrl', function ($scope) {
$scope.myStyle={};
$scope.myStyle.width = "150px";
$scope.value= "My value is anythinggggggggggggggggg";
})
.filter('splice', function () {
return function (value, max, tail) {
if (!value) return '';
max = parseInt(max, 10);
if (!max) return value;
if (value.length <= max) return value;
value = value.substr(0, max);
return value + (tail || ' …');
};
});
Share
Improve this question
edited Apr 4, 2015 at 7:44
Alex Man
asked Apr 4, 2015 at 7:30
Alex ManAlex Man
4,88619 gold badges105 silver badges188 bronze badges
1
- Check this jsfiddle/mohamedrias/pgc4pqn2, see if it satisfies your need – mohamedrias Commented Apr 4, 2015 at 8:00
1 Answer
Reset to default 6I've tried with a different appraoch for your problem. See if it helps:
Instead of passing the limit
to the filter, am passing the width
of the element.
<div class="test" ng-style="myStyle">{{value | splice:myStyle.width:' ...'}}</div>
Now inside your filter:
function (value, max, tail) {
if(isNaN(max))
max = parseInt(max.slice(0,-2),10)/12;
if (!value) return '';
max = parseInt(max, 10);
// rest of your code
Am checking if it's a string. In case its a string, assuming you're passing width
in format of 150px
. So slicing the px
from the string. we've 150
now.
Now taking the least font size of 8 and dividing it by 8. So now the limit will be based on the width of the element.
You can reuse the same filter
by passing the width as well as passing the limit directly. It will suit for both the scenarios now.
DEMO
Updated Answer
I've been looking at different solutions. As you've mentioned in ments that the font-size may vary. I guess better we go with simple CSS based approach in that case using text-overflow
.
.test {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Now whatever may be the font-size, the text will always be clipped. Now in whichever elements you want to have this feature, add specific css class clip-text
.
DEMO
本文标签: javascriptdynamic text length limit based on div widthStack Overflow
版权声明:本文标题:javascript - dynamic text length limit based on div width - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742407288a2469082.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论