rviscomi / trunk8

jQuery Truncation Plugin -- THIS PROJECT IS NO LONGER MAINTAINED
MIT License
703 stars 95 forks source link

Floating and positioned elements. #13

Closed zgood closed 11 years ago

zgood commented 12 years ago

This plugin does not work with floating or position elements, which makes it very limited in the senerios in which it can be used. Almost ALL of my dynamically created elements are floated or positioned, and those are the only elements I need to truncate text on. It's because the "getLineHeight" utility always comes back with "0" on those elements and then it just keeps truncating the string until there is nothing left. At least give an option to supply a line-height for those type of elements, because I know what the line-height I want.

zgood commented 12 years ago

I have update trunk8.js "getLineHeight" function to take in consideration floats and positions:

getLineHeight: function (elem) { var float = $(elem).css('float'); if (float !== 'none') { $(elem).css('float', 'none'); } var pos = $(elem).css('position'); if (pos === 'absolute') { $(elem).css('position', 'static'); }

        var html = $(elem).html(),
            wrapper_id = 'line-height-test',
            line_height;

        /* Set the content to a small single character and wrap. */
        $(elem).html('i').wrap('<div id="' + wrapper_id + '" />');

        /* Calculate the line height by measuring the wrapper.*/
        line_height = $('#' + wrapper_id).innerHeight();

        /* Remove the wrapper and reset the content. */
        $(elem).html(html).css({ 'float': float, 'position': pos }).unwrap();

        return line_height;
    }
rviscomi commented 12 years ago

Thanks @zgood! Could you submit a pull request so I can test and merge?