-
Notifications
You must be signed in to change notification settings - Fork 2
/
responsiveTruncator.js
69 lines (66 loc) · 2.1 KB
/
responsiveTruncator.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* jQuery Responsive Truncator Plugin
*
* https://github.com/jkeck/responsiveTruncator
*
* VERSION 0.0.2
*
**/
(function( $ ){
$.fn.responsiveTruncate = function(options){
var $this = this;
$(window).bind("resize", function(){
removeTruncation($this);
addTruncation($this);
});
addTruncation($this);
function addTruncation(el){
el.each(function(){
if($(".responsiveTruncate", $(this)).length == 0){
var parent = $(this);
var fontSize = $(this).css('font-size');
var lineHeight = $(this).css("line-height") ? $(this).css("line-height").replace('px','') : Math.floor(parseInt(fontSize.replace('px','')) * 1.5);
var total_lines = Math.ceil(parent.height() / lineHeight);
var settings = $.extend({
'lines' : 3,
'height' : null,
'more' : 'more',
'less' : 'less'
}, options);
var truncate_height;
if(settings.height){
truncate_height = settings.height;
}else{
truncate_height = (lineHeight * settings.lines);
}
if(parent.height() > truncate_height) {
var orig_content = parent.html();
parent.html("<div style='height: " + truncate_height + "px; overflow: hidden;' class='responsiveTruncate'></div>");
var truncate = $(".responsiveTruncate", parent);
truncate.html(orig_content);
truncate.after("<a class='responsiveTruncatorToggle' href='#'>" + settings.more + "</a>");
var toggle_link = $(".responsiveTruncatorToggle", parent);
toggle_link.click(function(){
var text = toggle_link.text() == settings.more ? settings.less : settings.more;
toggle_link.text(text);
if(truncate.height() == truncate_height){
truncate.css({height: '100%'})
}else{
truncate.css({height: truncate_height})
}
return false;
});
}
}
});
}
function removeTruncation(el){
el.each(function(){
if($(".responsiveTruncate", $(this)).length > 0){
$(this).html($(".responsiveTruncate", $(this)).html());
$(".responsiveTruncatorToggle", $(this)).remove();
}
});
}
};
})( jQuery );