1 | (function ( $ ) {
|
---|
2 |
|
---|
3 | /*
|
---|
4 | Plugin: ShowMore
|
---|
5 | author: [email protected]
|
---|
6 | */
|
---|
7 |
|
---|
8 | $.fn.showMore = function (options) {
|
---|
9 |
|
---|
10 | "use strict";
|
---|
11 |
|
---|
12 | var currentelem = 1;
|
---|
13 |
|
---|
14 | this.each(function(){
|
---|
15 |
|
---|
16 | var currentid = '';
|
---|
17 | var element = $(this);
|
---|
18 | var auto = parseInt(element.innerHeight())/2;
|
---|
19 | var fullheight = element.innerHeight();
|
---|
20 | var maxWidth = element.css('width');
|
---|
21 | var settings = $.extend({
|
---|
22 | minheight: auto,
|
---|
23 | buttontxtmore: "show more",
|
---|
24 | buttontxtless: "show less",
|
---|
25 | buttoncss: "showmore-button",
|
---|
26 | animationspeed: auto
|
---|
27 | }, options );
|
---|
28 |
|
---|
29 | element.attr('id') != undefined ? currentid = element.attr('id') : currentid = currentelem;
|
---|
30 | element.wrap( "<div id='showmore-"+currentid+"' data-showmore"+(maxWidth == '0px' ? "" : " style='max-width:"+maxWidth+";'")+"></div>" );
|
---|
31 |
|
---|
32 | if (element.parent().not('[data-showmore]')) {
|
---|
33 |
|
---|
34 | if (fullheight > settings.minheight) {
|
---|
35 |
|
---|
36 | element.css('min-height', settings.minheight).css('max-height', settings.minheight).css('overflow', 'hidden');
|
---|
37 | var showMoreButton = $("<div />", {
|
---|
38 | id: "showmore-button-"+currentid,
|
---|
39 | "class": settings.buttoncss,
|
---|
40 | click: function() {
|
---|
41 |
|
---|
42 | if (element.css('max-height') != 'none') {
|
---|
43 | element.css('height', settings.minheight).css('max-height', '').animate({height:fullheight}, settings.animationspeed, function () { showMoreButton.html(settings.buttontxtless); });
|
---|
44 | } else {
|
---|
45 | element.animate({height:settings.minheight}, settings.animationspeed, function () { showMoreButton.html(settings.buttontxtmore); element.css('max-height', settings.minheight); });
|
---|
46 | }
|
---|
47 | },
|
---|
48 | html: settings.buttontxtmore
|
---|
49 | });
|
---|
50 |
|
---|
51 | element.after(showMoreButton);
|
---|
52 |
|
---|
53 | }
|
---|
54 |
|
---|
55 | currentelem++;
|
---|
56 |
|
---|
57 | }
|
---|
58 |
|
---|
59 | });
|
---|
60 |
|
---|
61 | return this;
|
---|
62 |
|
---|
63 | };
|
---|
64 |
|
---|
65 | }(jQuery));
|
---|