Ellipsis in the middle of a text (Mac style)

后端 未结 15 2271
长情又很酷
长情又很酷 2020-11-29 03:04

I need to implement ellipsis (\"...\") in the middle of a text within a resizable element. Here is what it might look like. So,

\"Lorem ipsum do         


        
15条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 03:24

    I just created a function that can trim at the middle, nearEnd and End but havent been tested yet because I finally was needing it at the server side

    //position acceptable values : middle, end, closeEnd
    function AddElipsis(input, maxChars, position) {
        if (typeof input === 'undefined') {
            return "";
        }
        else if (input.length <= maxChars) {
            return input;
        }
        else {
            if (position == 'middle') {
                var midPos = Math.floor(maxChars / 2) - 2;
                return input.substr(0, midPos) + '...' + input.substr(input.length - midPos, input.length);
            }
            else if (position == 'closeEnd') {
                var firstPart = Math.floor(maxChars * 0.80) - 2;
                var endPart = Math.floor(maxChars * 0.20) - 2;
                return input.substr(0, firstPart) + '...' + input.substr(input.length - endPart, input.length);
            }
            else {
                return input.substr(0, maxChars - 3) + '...';
            }
        }
    }
    

提交回复
热议问题