diff options
author | jlehner <jakelehner@air-watch.com> | 2013-12-16 14:38:35 -0500 |
---|---|---|
committer | jlehner <jakelehner@air-watch.com> | 2013-12-16 14:38:35 -0500 |
commit | f43596c21288ee64a8b0b23061c8b8fcc34614a7 (patch) | |
tree | f86fcc1eef200797f0b3117b70500fcc33cd37d6 | |
parent | db4c5f4b613e6e9a0a1b25d14263c41223073492 (diff) | |
download | Readmore.js-f43596c21288ee64a8b0b23061c8b8fcc34614a7.zip Readmore.js-f43596c21288ee64a8b0b23061c8b8fcc34614a7.tar.gz Readmore.js-f43596c21288ee64a8b0b23061c8b8fcc34614a7.tar.bz2 |
Add height margin option to handle situations where the expanded div size is just barely larger than maxHeight.
-rw-r--r-- | readmore.js | 18 | ||||
-rw-r--r-- | readmore.min.js | 6 |
2 files changed, 8 insertions, 16 deletions
diff --git a/readmore.js b/readmore.js index 0bf2119..13a0001 100644 --- a/readmore.js +++ b/readmore.js @@ -1,16 +1,10 @@ -/*! - * 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, + heightMargin: 15, moreLink: '<a href="#">Read More</a>', lessLink: '<a href="#">Close</a>', embedCSS: true, @@ -29,6 +23,7 @@ this.options = $.extend( {}, defaults, options); $(this.element).data('max-height', this.options.maxHeight); + $(this.element).data('height-margin', this.options.heightMargin); delete(this.options.maxHeight); @@ -63,8 +58,9 @@ $(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'); - + maxHeight = (current.css('max-height').replace(/[^-\d\.]/g, '') > current.data('max-height')) ? current.css('max-height').replace(/[^-\d\.]/g, '') : current.data('max-height'), + hightMargin = current.data('height-margin'); +console.log(current.data('height-margin')); current.addClass('readmore-js-section'); if(current.css('max-height') != "none") { @@ -73,7 +69,7 @@ current.data("boxHeight", current.outerHeight(true)); - if(current.outerHeight(true) < maxHeight) { + if(current.outerHeight(true) < maxHeight + hightMargin) { // The block is shorter than the limit, so there's no need to truncate it. return true; } @@ -139,4 +135,4 @@ }); } } -})(jQuery); +})(jQuery);
\ No newline at end of file diff --git a/readmore.min.js b/readmore.min.js index f5157b2..a3b89b7 100644 --- a/readmore.min.js +++ b/readmore.min.js @@ -1,5 +1 @@ -(function(d){function g(c,a){this.element=c;this.options=d.extend({},h,a);d(this.element).data("max-height",this.options.maxHeight);delete this.options.maxHeight;if(this.options.embedCSS&&!k){var b=".readmore-js-toggle, .readmore-js-section { "+this.options.sectionCSS+" } .readmore-js-section { overflow: hidden; }",e=document.createElement("style");e.type="text/css";e.styleSheet?e.styleSheet.cssText=b:e.appendChild(document.createTextNode(b));document.getElementsByTagName("head")[0].appendChild(e); -k=!0}this._defaults=h;this._name=f;this.init()}var f="readmore",h={speed:100,maxHeight:200,moreLink:'<a href="#">Read More</a>',lessLink:'<a href="#">Close</a>',embedCSS:!0,sectionCSS:"display: block; width: 100%;",beforeToggle:function(){},afterToggle:function(){}},k=!1;g.prototype={init:function(){var c=this;d(this.element).each(function(){var a=d(this),b=a.css("max-height").replace(/[^-\d\.]/g,"")>a.data("max-height")?a.css("max-height").replace(/[^-\d\.]/g,""):a.data("max-height");a.addClass("readmore-js-section"); -"none"!=a.css("max-height")&&a.css("max-height","none");a.data("boxHeight",a.outerHeight(!0));if(a.outerHeight(!0)<b)return!0;a.after(d(c.options.moreLink).on("click",function(b){c.toggleSlider(this,a,b)}).addClass("readmore-js-toggle"));a.data("sliderHeight",b);a.css({height:b})})},toggleSlider:function(c,a,b){b.preventDefault();var e=this,f=newLink="";b=!1;f=d(a).data("sliderHeight");d(a).height()==f?(f=d(a).data().boxHeight+"px",newLink="lessLink",b=!0):newLink="moreLink";e.options.beforeToggle(c, -a,b);d(a).animate({height:f},{duration:e.options.speed});d(c).replaceWith(d(e.options[newLink]).on("click",function(b){e.toggleSlider(this,a,b)}).addClass("readmore-js-toggle"));e.options.afterToggle(c,a,b)}};d.fn[f]=function(c){var a=arguments;if(void 0===c||"object"===typeof c)return this.each(function(){d.data(this,"plugin_"+f)||d.data(this,"plugin_"+f,new g(this,c))});if("string"===typeof c&&"_"!==c[0]&&"init"!==c)return this.each(function(){var b=d.data(this,"plugin_"+f);b instanceof g&&"function"=== -typeof b[c]&&b[c].apply(b,Array.prototype.slice.call(a,1))})}})(jQuery); +(function(e){function i(i,s){this.element=i;this.options=e.extend({},n,s);e(this.element).data("max-height",this.options.maxHeight);e(this.element).data("height-margin",this.options.heightMargin);delete this.options.maxHeight;if(this.options.embedCSS&&!r){var o=".readmore-js-toggle, .readmore-js-section { "+this.options.sectionCSS+" } .readmore-js-section { overflow: hidden; }";(function(e,t){var n=e.createElement("style");n.type="text/css";if(n.styleSheet){n.styleSheet.cssText=t}else{n.appendChild(e.createTextNode(t))}e.getElementsByTagName("head")[0].appendChild(n)})(document,o);r=true}this._defaults=n;this._name=t;this.init()}var t="readmore",n={speed:100,maxHeight:200,heightMargin:15,moreLink:'<a href="#">Read More</a>',lessLink:'<a href="#">Close</a>',embedCSS:true,sectionCSS:"display: block; width: 100%;",startOpen:false,beforeToggle:function(){},afterToggle:function(){}},r=false;i.prototype={init:function(){var t=this;e(this.element).each(function(){var n=e(this),r=n.css("max-height").replace(/[^-\d\.]/g,"")>n.data("max-height")?n.css("max-height").replace(/[^-\d\.]/g,""):n.data("max-height"),i=n.data("height-margin");console.log(n.data("height-margin"));n.addClass("readmore-js-section");if(n.css("max-height")!="none"){n.css("max-height","none")}n.data("boxHeight",n.outerHeight(true));if(n.outerHeight(true)<r+i){return true}else{n.data("sliderHeight",r);var s=t.options.startOpen?t.options.lessLink:t.options.moreLink;n.after(e(s).on("click",function(e){t.toggleSlider(this,n,e)}).addClass("readmore-js-toggle"));if(!t.options.startOpen){n.css({height:r})}}})},toggleSlider:function(t,n,r){r.preventDefault();var i=this,s=newLink="",o=false,u=e(n).data("sliderHeight");if(e(n).height()==u){s=e(n).data().boxHeight+"px";newLink="lessLink";o=true}else{s=u;newLink="moreLink"}i.options.beforeToggle(t,n,o);e(n).animate({height:s},{duration:i.options.speed});e(t).replaceWith(e(i.options[newLink]).on("click",function(e){i.toggleSlider(this,n,e)}).addClass("readmore-js-toggle"));i.options.afterToggle(t,n,o)}};e.fn[t]=function(n){var r=arguments;if(n===undefined||typeof n==="object"){return this.each(function(){if(!e.data(this,"plugin_"+t)){e.data(this,"plugin_"+t,new i(this,n))}})}else if(typeof n==="string"&&n[0]!=="_"&&n!=="init"){return this.each(function(){var s=e.data(this,"plugin_"+t);if(s instanceof i&&typeof s[n]==="function"){s[n].apply(s,Array.prototype.slice.call(r,1))}})}}})(jQuery)
\ No newline at end of file |