Haircut trims text down to fit into its container.

Get it

Download .zip Download .tar.gz View on GitHub

Demo

Usage

First, you will need to change up some of your HTML. Haircut expects an element with the full text contained within the title attribute like so:

<abbr title="My excrutiatingly long string"></abbr>

You will then need to wrap that and call Haircut on the parent element:

<h1><abbr title="My excrutiatingly long string"></abbr></h1>

Initialize by calling .haircut(). If the layout changes and your Haircut element might change, call .stringResize(). Like so:

$('h1').haircut();

$(window).resize(function () {
  $('h1').stringResize();
});

You can also give it a few options:

$('h2').haircut({
  // The more haircut items on the page,
  // the more it jitters during animation.
  // Adding padding reduces jitter but
  // gives you less usable space.
  jitterPadding: 30,

  // Decide which part of the string to trim.
  // Can be "start", "middle" or "end".
  placement: 'middle'
});

It's also recommended that you style 2 CSS selectors: ._LVhaircutExpand and ._LVshowHaircutExpand. If configured, Haircut will overlay the full text when a Haircut string is hovered over. See below for an example.

If the containing element has an anchor tag, that will be brought along for the ride. That way long URLs can be truncated at no expense of usability.

Installation

Just include jquery.haircut.js as a script tag on the page. Bonus points for incorporating it into your asset pipeline.

In Action

Here's a little video of Haircut in action on the LayerVault Directory page.

License

Copyright 2013 LayerVault Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.