-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.equalBreak.js
48 lines (38 loc) · 997 Bytes
/
jquery.equalBreak.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* jQuery Equal Line Breaking v1.0
* A jQuery plugin that breaks a long string into multiple equal lines of text
* Copyright (c) 2013 Spiros Martzoukos
*
* https://github.com/martzoukos/equal-break
*
* Depends:
* - jQuery 1.3+
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
*/
(function( $ ) {
$.fn.equalBreak = function( options ) {
var settings = $.extend({
lines: 2
}, options);
return this.each(function(){
var $el = $(this),
text = $el.html(),
words = text.split(" "),
breakPoints = parseInt( (text.length / settings.lines), 10),
breakIncr = breakPoints,
letterCount = 0,
result = "";
for (i=0; i<words.length; i++) {
if ( letterCount >= breakPoints ) {
result += "<br>";
breakPoints += breakIncr;
}
result += " " + words[i];
letterCount += words[i].length + 1;
}
$el.html( result );
});
};
}( jQuery ));