Tuesday, June 11, 2013

trimToParentHeight jquery plugin( trim string to fit parent container)

//trim string to fit the height of its parent container
//has to pass the ellipsis that could include html tag to append to the end of string
$.fn.trimToParentHeight = function (options) {
    return this.each(function (index, text) {
        $text = $(text);
        var temp = $text.text();
        var moreLink = $text.attr('data-link');
        $text.append('<a href="' + moreLink + '"> ' + options.ellipsis + '</a>');

        if ($text.parent().height() < $text.outerHeight()) {

            while ($text.parent().height() < $text.outerHeight()) {
                $text.text(temp = temp.substr(0, temp.length - 1));
            }

            var appendstr = '... <a href="' + moreLink + '"> ' + options.ellipsis + '</a>';
            $text.text(temp = temp.substr(0, temp.length - options.ellipsis.length - 5));
            $text.append(appendstr);
        }


    });
}

No comments:

Post a Comment