Skip to content

Instantly share code, notes, and snippets.

View cscheng's full-sized avatar

Chi Shang Cheng cscheng

View GitHub Profile
@cscheng
cscheng / jquery.truncatelines.js
Created April 7, 2011 07:39
A simple jQuery plugin to truncate a piece of text to a predefined amount of lines. It assumes pixel values of both the container and its line-height. Useful when you have multiple boxes with text in your design that require equal heights.
$.fn.truncateLines = function(options) {
options = $.extend($.fn.truncateLines.defaults, options);
return this.each(function(index, container) {
container = $(container);
var containerLineHeight = Math.ceil(parseFloat(container.css('line-height')));
var maxHeight = options.lines * containerLineHeight;
var truncated = false;
var truncatedText = $.trim(container.text());
var overflowRatio = container.height() / maxHeight;