forked from jedfoster/Readmore.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadmore.js
143 lines (114 loc) · 4.2 KB
/
readmore.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*!
* Readmore.js jQuery plugin
* Author: @jed_foster
* Project home: jedfoster.github.io/Readmore.js
* Licensed under the MIT license
*/
;(function($) {
var readmore = 'readmore',
defaults = {
speed: 100,
maxHeight: 200,
moreLink: '<a href="#">Read More</a>',
lessLink: '<a href="#">Close</a>',
embedCSS: true,
sectionCSS: 'display: block; width: 100%;',
startOpen: false,
// callbacks
beforeToggle: function(){},
afterToggle: function(){}
},
cssEmbedded = false;
function Readmore( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options);
$(this.element).data('max-height', this.options.maxHeight);
delete(this.options.maxHeight);
if(this.options.embedCSS && ! cssEmbedded) {
var styles = '.readmore-js-toggle, .readmore-js-section { ' + this.options.sectionCSS + ' } .readmore-js-section { overflow: hidden; }';
(function(d,u) {
var css=d.createElement('style');
css.type = 'text/css';
if(css.styleSheet) {
css.styleSheet.cssText = u;
}
else {
css.appendChild(d.createTextNode(u));
}
d.getElementsByTagName("head")[0].appendChild(css);
}(document, styles));
cssEmbedded = true;
}
this._defaults = defaults;
this._name = readmore;
this.init();
}
Readmore.prototype = {
init: function() {
var $this = this;
$(this.element).each(function() {
var current = $(this),
maxHeight = (current.css('max-height').replace(/[^-\d\.]/g, '') > current.data('max-height')) ? current.css('max-height').replace(/[^-\d\.]/g, '') : current.data('max-height');
current.addClass('readmore-js-section');
if(current.css('max-height') != "none") {
current.css("max-height", "none");
}
current.data("boxHeight", current.outerHeight(true));
if(current.outerHeight(true) < maxHeight) {
// The block is shorter than the limit, so there's no need to truncate it.
return true;
}
else {
current.data('sliderHeight', maxHeight);
var useLink = $this.options.startOpen ? $this.options.lessLink : $this.options.moreLink;
current.after($(useLink).on('click', function(event) { $this.toggleSlider(this, current, event) }).addClass('readmore-js-toggle'));
if(!$this.options.startOpen) {
current.css({height: maxHeight});
}
}
});
},
toggleSlider: function(trigger, element, event)
{
event.preventDefault();
var $this = this,
newHeight = newLink = '',
more = false,
sliderHeight = $(element).data('sliderHeight');
if ($(element).height() == sliderHeight) {
newHeight = $(element).data().boxHeight + "px";
newLink = 'lessLink';
more = true;
}
else {
newHeight = sliderHeight;
newLink = 'moreLink';
}
// Fire beforeToggle callback
$this.options.beforeToggle(trigger, element, more);
$(element).animate({"height": newHeight}, {duration: $this.options.speed, complete: function() {
// Fire afterToggle callback
$this.options.afterToggle(trigger, element, more);
}
});
$(trigger).replaceWith($($this.options[newLink]).on('click', function(event) { $this.toggleSlider(this, element, event) }).addClass('readmore-js-toggle'));
}
};
$.fn[readmore] = function( options ) {
var args = arguments;
if (options === undefined || typeof options === 'object') {
return this.each(function () {
if (!$.data(this, 'plugin_' + readmore)) {
$.data(this, 'plugin_' + readmore, new Readmore( this, options ));
}
});
} else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
return this.each(function () {
var instance = $.data(this, 'plugin_' + readmore);
if (instance instanceof Readmore && typeof instance[options] === 'function') {
instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) );
}
});
}
}
})(jQuery);