├── README.md
├── examples
├── css-or-js.html
└── examples.html
├── lib
└── jquery.text-overflow.js
└── todo
/README.md:
--------------------------------------------------------------------------------
1 | ## jQuery Text Overflow plugin
2 |
3 | This jQuery plugin implements a simplified version of the
4 | [CSS3 text-overflow](https://developer.mozilla.org/en/CSS/text-overflow)
5 | property. The `text-overflow` property allows stylesheet authors to
6 | specify how and where text should be clipped. This is usually done
7 | by adding an ellipsis character "`…`" or three dots at the point
8 | the text should be cut off. Various browsers support this property,
9 | such as Opera, Internet Explorer and Safari. Firefox unfortunately
10 | does not. However, using this plugin you can simulate this
11 | functionality (well, as close as you can get without a native
12 | implementation.)
13 |
14 | To use it you simply call the `textOverflow` method on elements you
15 | want clipped. Clipping is only applied when the browser does not
16 | support it natively. The following is an example of how to clip all
17 | header elements on a page using the default plugin settings.
18 |
19 | $('h1').textOverflow();
20 |
21 | The `textOverflow` method optionally supports an options object containing: the
22 | string to use for clipping the text, and a boolean to determine
23 | whether the plugin should automatically update the clipping when a
24 | change in the document has been detected.
25 |
26 | textOverflow(options)
27 |
28 | The options object can contain the following properties:
29 |
30 |
`str`
31 |
The string to append to the content before it is clipped. Defaults to the ellipsis character "`…`". May also contain HTML, but the string itself is not subject to being clipped.
32 |
`autoUpdate`
33 |
Automatically update the clipped text when the available space for the text becomes smaller or larger. Defaults to false (because it incurs some extra costs.)
34 |
`trim`
35 |
True to remove trailing white-space at the point where content is clipped. False to leave the white-space. Defaults to true.
36 |
`title`
37 |
True to add the full text as a title attribute on the clipped element. Defaults to false.
38 |
`className`
39 |
Class name to add to the element whose content is clipped. Note that elements whose content fit in the container will not have this class name added.
40 |
`wholeWord`
41 |
True to clip content only at whole word boundaries. False might clip a word anywhere. Defaults to false.
42 |
43 |
44 | The following example shows how to replace the default ellipsis
45 | character on all list elements with a three dot version, and turn
46 | on automatic updating.
47 |
48 | $('li').textOverflow({
49 | str: '...',
50 | autoUpdate: true
51 | });
52 |
53 | Please see the [examples page](examples/examples.html) for more examples on
54 | how to use the plugin.
55 |
56 | ## Frequently asked questions
57 |
58 | Q: Does this plugin support HTML elements inside the clipped content?
59 |
60 | A: Yes.
61 |
62 | Q: How do I use this in combination with browsers that support the
63 | text-overflow property?
64 |
65 | A: Simply use the CSS3 `text-overflow` property as you would normally,
66 | e.g.
67 |
68 | overflow: hidden;
69 | white-space: nowrap;
70 | text-overflow: ellipsis;
71 |
72 | Then also call the `textOverflow` plugin method on the same
73 | elements. When a browser has native support for the `text-overflow`
74 | property, the text overflow plugin will let the browse handle it
75 | natively, and otherwise it will apply its own text truncation code.
76 | For more details, see the
77 | [CSS and JavaScript text overflow example](examples/css-or-js.html), or the
78 | [Quirkmode's article on the text-overflow property](http://www.quirksmode.org/css/textoverflow.html).
79 |
80 | Q: How do I restore the original contents of the clipped elements?
81 |
82 | A: It depends on what your use case is. If you want to show more or
83 | less of the clipped element based on browser or element resize
84 | events, simply turn the `autoUpdate` functionality on. If you want
85 | to manually restore the original content, you'll have to store it
86 | somewhere yourself before applying the text overflow plugin.
87 |
88 | Q: Text overflow doesn't work when applied to hidden elements (using
89 | `display: none`.)
90 |
91 | A: This is caused by the browser taking the elements out of the page
92 | flow. When an element is not in the page flow it has zero width and
93 | height. The text overflow plugin uses the width and height to
94 | calculate where to clip the text, so it can not do its work when an
95 | element is hidden. There are two approaches to work around this
96 | problem: the most simple solution is to only call the text overflow
97 | plugin after you display the hidden elements. Alternatively you
98 | could position the hidden elements offscreen and set its
99 | `visibility` property to `hidden` and then measure it.
100 |
101 | ## Credits
102 |
103 | -
104 | [Devon Govett](http://devongovett.wordpress.com/2009/04/06/text-overflow-ellipsis-for-firefox-via-jquery/)
105 | ― Original plugin.
106 |
107 | -
108 | Andrew A. Kononykhin ― Bug fix for markup reset on cloned
109 | elements.
110 |
111 | -
112 | Micky Hulse ― Bug report on ellipsis character encoding & incorrect
113 | DOM tree truncation.
114 |
115 | -
116 | Edward Shtern ― Suggestions for: className, title and wholeWord options.
117 |
118 |
119 | ### License
120 |
121 | This plugin is licensed under the
122 | [new BSD license](http://www.bramstein.com/licenses/BSD.txt). To summarize the license; the
123 | plugin is completely free for commercial and non-commercial use and
124 | you can do with it whatever you want, except claim it as your own
125 | work.
126 |
127 |
--------------------------------------------------------------------------------
/examples/css-or-js.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Text Overflow Example
6 |
7 |
8 |
13 |
25 |
26 |
27 |
28 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean quis elit in massa pretium tempus. Curabitur vehicula aliquam massa a pellentesque. Ut ut orci urna, vel pulvinar neque.
29 |
Donec diam dolor, sollicitudin eu congue eu, consectetur et felis. Fusce est odio, cursus eget feugiat sed, semper eu ante. Nullam quis mauris et turpis laoreet laoreet quis non elit. Nunc elementum laoreet elit, ac porta nunc ullamcorper et. Phasellus ornare dapibus sem, sed posuere eros pellentesque eu. Donec et quam quam. Aliquam rhoncus mollis rutrum.
30 |
Cras fringilla, nisl vitae posuere tincidunt, libero sem mattis lectus, eget consequat leo dolor ac tortor. Fusce facilisis metus eu odio commodo pulvinar. Ut sit amet ligula sed purus tincidunt tincidunt. Curabitur tempus placerat interdum. Mauris viverra, mauris et luctus malesuada, urna eros scelerisque ante, sed imperdiet velit velit nec augue. Donec arcu ante, tincidunt eget facilisis et, condimentum id magna. Donec ipsum quam, suscipit a ornare quis, vestibulum ut lectus. Fusce ultrices laoreet varius. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam diam ligula, dapibus eget scelerisque et, congue quis mauris.
some link | a too long text which should be truncated by this plugin. What it however should not do is remove the space from the start of this sentence.
This first example shows the default settings at work. It just calls the textOverflow method on the list elements without any parameters. Note that container elements and inline elements are properly conserved and lines are truncated after words (i.e. trailing spaces are removed.)
42 |
43 |
44 | $('#default li').textOverflow();
45 |
46 |
47 |
48 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean quis elit in massa pretium tempus. Curabitur vehicula aliquam massa a pellentesque. Ut ut orci urna, vel pulvinar neque.
49 |
Donec diam dolor, sollicitudin eu congue eu, consectetur et felis. Fusce est odio, cursus eget feugiat sed, semper eu ante. Nullam quis mauris et turpis laoreet laoreet quis non elit. Nunc elementum laoreet elit, ac porta nunc ullamcorper et. Phasellus ornare dapibus sem, sed posuere eros pellentesque eu. Donec et quam quam. Aliquam rhoncus mollis rutrum.
50 |
Cras fringilla, nisl vitae posuere tincidunt, libero sem mattis lectus, eget consequat leo dolor ac tortor. Fusce facilisis metus eu odio commodo pulvinar. Ut sit amet ligula sed purus tincidunt tincidunt. Curabitur tempus placerat interdum. Mauris viverra, mauris et luctus malesuada, urna eros scelerisque ante, sed imperdiet velit velit nec augue. Donec arcu ante, tincidunt eget facilisis et, condimentum id magna. Donec ipsum quam, suscipit a ornare quis, vestibulum ut lectus. Fusce ultrices laoreet varius. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam diam ligula, dapibus eget scelerisque et, congue quis mauris.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean quis elit in massa pretium tempus. Curabitur vehicula aliquam massa a pellentesque. Ut ut orci urna, vel pulvinar neque.
67 |
Donec diam dolor, sollicitudin eu congue eu, consectetur et felis. Fusce est odio, cursus eget feugiat sed, semper eu ante. Nullam quis mauris et turpis laoreet laoreet quis non elit. Nunc elementum laoreet elit, ac porta nunc ullamcorper et. Phasellus ornare dapibus sem, sed posuere eros pellentesque eu. Donec et quam quam. Aliquam rhoncus mollis rutrum.
68 |
Cras fringilla, nisl vitae posuere tincidunt, libero sem mattis lectus, eget consequat leo dolor ac tortor. Fusce facilisis metus eu odio commodo pulvinar. Ut sit amet ligula sed purus tincidunt tincidunt. Curabitur tempus placerat interdum. Mauris viverra, mauris et luctus malesuada, urna eros scelerisque ante, sed imperdiet velit velit nec augue. Donec arcu ante, tincidunt eget facilisis et, condimentum id magna. Donec ipsum quam, suscipit a ornare quis, vestibulum ut lectus. Fusce ultrices laoreet varius. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam diam ligula, dapibus eget scelerisque et, congue quis mauris.
69 |
70 |
The next example uses the autoUpdate parameter to the textOverflow method to automatically update the clipping when the parent container is resized. You can try this out by pressing the "Resize parent" button (code for this is not shown.)
It passes null as the first parameter, indicating we wish to use the default value for clipping (the ellipsis character.)
79 |
80 |
81 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean quis elit in massa pretium tempus. Curabitur vehicula aliquam massa a pellentesque. Ut ut orci urna, vel pulvinar neque.
82 |
Donec diam dolor, sollicitudin eu congue eu, consectetur et felis. Fusce est odio, cursus eget feugiat sed, semper eu ante. Nullam quis mauris et turpis laoreet laoreet quis non elit. Nunc elementum laoreet elit, ac porta nunc ullamcorper et. Phasellus ornare dapibus sem, sed posuere eros pellentesque eu. Donec et quam quam. Aliquam rhoncus mollis rutrum.
83 |
Cras fringilla, nisl vitae posuere tincidunt, libero sem mattis lectus, eget consequat leo dolor ac tortor. Fusce facilisis metus eu odio commodo pulvinar. Ut sit amet ligula sed purus tincidunt tincidunt. Curabitur tempus placerat interdum. Mauris viverra, mauris et luctus malesuada, urna eros scelerisque ante, sed imperdiet velit velit nec augue. Donec arcu ante, tincidunt eget facilisis et, condimentum id magna. Donec ipsum quam, suscipit a ornare quis, vestibulum ut lectus. Fusce ultrices laoreet varius. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam diam ligula, dapibus eget scelerisque et, congue quis mauris.