jQuery Truncation Plugin
Pull request Compare This branch is 63 commits behind rviscomi:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
LICENSE
README.md
demo.html
trunk8.js

README.md

trunk8

About

trunk8 is a text truncation extension to jQuery. When applied to a large block of text, trunk8 will cut off just enough text to prevent it from spilling over.

Unlike conventional truncation that just limits the character length of text, trunk8 measures the content area for spill-over and intelligently chooses the text that best fits in the given space.

Play with the live demo.

Usage

Default Settings

$('.too-long').trunk8(); // Lorem ipsum dolor sit amet, consect…

By default, trunk8 will add an ellipsis (…) after the truncated text.

Method Invocation

$('.too-long').trunk8('update'); // Lorem ipsum dolor sit amet, consect…

To invoke built-in trunk8 methods, supply the method name as a string. See the section below entitled Methods for a full specification.

Custom Settings

$('.too-long').trunk8({
    fill: '[snip]',
    side: 'center'
}); // Lorem ipsum dol[snip]id est laborum.

You may change the settings at any time by passing an object to trunk8. In the example above, the filler text is overwritten from an ellipsis to a custom string and the placement of the truncation is overwritten from the right side to the center. Continue reading the section below for a full list of customizable settings.

Settings

  • fill (Default: '…') The string to insert in place of the omitted text. This value may include HTML.
  • lines (Default: 1) The number of lines of text-wrap to tolerate before truncating. This value must be an integer greater than or equal to 1.
  • side (Default: 'right') The side of the text from which to truncate. Valid values include 'center', 'left', and 'right'.
  • tooltip (Default: true) When true, the title attribute of the targeted HTML element will be set to the original, untruncated string. Valid values include true and false.
  • width (Default: 'auto') The width, in characters, of the desired text. When set to 'auto', trunk8 will maximize the amount of text without spilling over.

Public Methods

[Constructor] Initializes the settings and immediately truncates the targeted HTML element. This method is called when arguments are omitted and when the first argument is an object. When supplied with an object, trunk8 will merge the user-defined settings with the predefined settings object and immediately truncate the targeted HTML element with the custom settings.

/* default settings */
$('.too-long').trunk8();

/* custom settings */
$('.too-long').trunk8({
   lines: 2
});

update Updates the text value of the targeted HTML elements while maintaining truncation. This method can be used to recalculate the truncation after a reflow, especially after changing the text itself or its container size.

$('.too-long').trunk8('update');

getSettings Returns an object representation of the settings currently in effect. Because a return value is expected, this method does not promote the jQuery pattern of method chaining:

/* good */
var setings = $('.too-long').trunk8('getSettings'); // {...}

/* bad */
$('.too-long').trunk8('getSettings').addClass('wrong'); // error