1. 程式人生 > >html 文字溢位解決方案

html 文字溢位解決方案

下面附兩種解決多行文字溢位的方案

    $('.fig').each(function (i) {
        var divH = $(this).height();
        var $p = $('p', $(this)).eq(0);
        while ($p.outerHeight() > divH) {
            $p.text($p.text().replace(/(\s)*([a-zA-Z0-9]|\W)(\.\.\.)?$/, "..."))
        }

    });

    //這種方式不如第一種好~
    var
Overflow = (function () { function _text() { this.ele = arguments[0]; } _text.prototype = { //根據字型大小 行高 求出 clamp deal: function () { var height = parseInt($('#pp').css('height')); var lineHeight = this.ele.css('line-height'
); var fontSize = this.ele.css('fontSize'); var num = (height / (parseInt(fontSize) + (parseInt(lineHeight) - parseInt(fontSize)))).toString(); alert(num); this.ele.css('-webkit-line-clamp', function () { return num.indexOf('.'
) > 0?num.substring(0, num.indexOf('.')):num; }); } }; return _text; })(); new Overflow($('#pp')).deal();