<title>Readmore.js: jQuery plugin for long blocks of text</title>
<metaname="description"content="A smooth, lightweight jQuery plugin for collapsing and expanding long blocks of text with “Read more” and “Close” links.">
<p>A smooth, responsive jQuery plugin for collapsing and expanding long blocks of text with “Read more” and “Close” links.</p>
</header>
<sectionid="info">
<p>The markup Readmore.js requires is so simple, you can probably use it with your existing HTML—there’s no need for complicated sets of <code>div</code>’s or hardcoded classes, just call <code>.readmore()</code> on the element containing your block of text and Readmore.js takes care of the rest. Readmore.js plays well in a responsive environment, too.</p>
<p>Readmore.js is tested with—and supported on—all versions of jQuery greater than 1.9.1. All the “good” browsers are supported, as well as IE10+; IE8 & 9 <em>should</em> work, but are not supported and the experience will not be ideal.</p>
<p>If the element has a <code>max-height</code> CSS property, Readmore.js will use that value rather than the value of the <code>collapsedHeight</code> option.</p>
<p>The <code>beforeToggle</code> and <code>afterToggle</code> callbacks both receive the same arguments: <code>trigger</code>, <code>element</code>, and <code>expanded</code>.</p>
<p>The <code>blockProcessed</code> callback receives <code>element</code> and <code>collapsable</code>.</p>
<ul>
<li><code>element</code>: the block that has just been processed</li>
<li><code>collapsable</code>: Boolean; <code>false</code> means the block was shorter than the specified minimum <code>collapsedHeight</code>—the block will not have a "Read more" link</li>
<p>Here’s an example of how you could use the <code>afterToggle</code> callback to scroll back to the top of a block when the “Close” link is clicked.</p>
<p>Readmore.js is designed to use CSS for as much functionality as possible: collapsed height can be set in CSS with the <code>max-height</code> property; “collapsing” is achieved by setting <code>overflow: hidden</code> on the containing block and changing the <code>height</code> property; and, finally, the expanding/collapsing animation is done with CSS3 transitions.</p>
<p>By default, Readmore.js inserts the following CSS, in addition to some transition-related rules:</p>
<p>If you want to include the necessary styling in your site’s stylesheet, you can disable the dynamic embedding by setting <code>embedCSS</code> to <code>false</code>:</p>
<h3id="mediaqueriesandothercsstricks">Media queries and other CSS tricks:</h3>
<p>If you wanted to set a <code>maxHeight</code> based on lines, you could do so in CSS with something like:</p>
<pre><codeclass="css">body {
font: 16px/1.5 sans-serif;
}
/* Show only 4 lines in smaller screens */
article {
max-height: 6em; /* (4 * 1.5 = 6) */
}</code></pre>
<p>Then, with a media query you could change the number of lines shown, like so:</p>
<pre><codeclass="css">/* Show 8 lines on larger screens */
@media screen and (min-width: 640px) {
article {
max-height: 12em;
}
}</code></pre>
<h2id="contributing">Contributing</h2>
<p>Pull requests are always welcome, but not all suggested features will get merged. Feel free to contact me if you have an idea for a feature.</p>
<p>Pull requests should include the minified script and this readme and the demo HTML should be updated with descriptions of your new feature. </p>
<p>You’ll need NPM:</p>
<pre><code>$ npm install</code></pre>
<p>Which will install the necessary development dependencies. Then, to build the minified script:</p>
<pre><code>$ gulp compress</code></pre>
</section>
<h1>Demo</h1>
<sectionid="demo">
<article>
<h2>Artisanal Narwahls</h2>
<p>From this distant vantage point, the Earth might not seem of any particular interest. But for us, it's different. Consider again that dot. That's here. That's home. That's us. On it everyone you love, everyone you know, everyone you ever heard of, every human being who ever was, lived out their lives. The aggregate of our joy and suffering, thousands of confident religions, ideologies, and economic doctrines, every hunter and forager, every hero and coward, every creator and destroyer of civilization, every king and peasant, every young couple in love, every mother and father, hopeful child, inventor and explorer, every teacher of morals, every corrupt politician, every "superstar," every "supreme leader," every saint and sinner in the history of our species lived there– on a mote of dust suspended in a sunbeam.</p>
<p>Space, the final frontier. These are the voyages of the starship Enterprise. Its five year mission: to explore strange new worlds, to seek out new life and new civilizations, to boldly go where no man has gone before!</p>
<p>Here's how it is: Earth got used up, so we terraformed a whole new galaxy of Earths, some rich and flush with the new technologies, some not so much. Central Planets, them was formed the Alliance, waged war to bring everyone under their rule; a few idiots tried to fight it, among them myself. I'm Malcolm Reynolds, captain of Serenity. Got a good crew: fighters, pilot, mechanic. We even picked up a preacher, and a bona fide companion. There's a doctor, too, took his genius sister out of some Alliance camp, so they're keeping a low profile. You got a job, we can do it, don't much care what it is.</p>
<p>Space, the final frontier. These are the voyages of the starship Enterprise. Its five year mission: to explore strange new worlds, to seek out new life and new civilizations, to boldly go where no man has gone before!</p>
</article>
<article>
<h2>Portland Leggings</h2>
<p>Here's how it is: Earth got used up, so we terraformed a whole new galaxy of Earths, some rich and flush with the new technologies, some not so much. Central Planets, them was formed the Alliance, waged war to bring everyone under their rule; a few idiots tried to fight it, among them myself. I'm Malcolm Reynolds, captain of Serenity. Got a good crew: fighters, pilot, mechanic. We even picked up a preacher, and a bona fide companion. There's a doctor, too, took his genius sister out of some Alliance camp, so they're keeping a low profile. You got a job, we can do it, don't much care what it is.</p>
<p>I am Duncan Macleod, born 400 years ago in the Highlands of Scotland. I am Immortal, and I am not alone. For centuries, we have waited for the time of the Gathering when the stroke of a sword and the fall of a head will release the power of the Quickening. In the end, there can be only one.</p>
<p>From this distant vantage point, the Earth might not seem of any particular interest. But for us, it's different. Consider again that dot. That's here. That's home. That's us. On it everyone you love, everyone you know, everyone you ever heard of, every human being who ever was, lived out their lives. The aggregate of our joy and suffering, thousands of confident religions, ideologies, and economic doctrines, every hunter and forager, every hero and coward, every creator and destroyer of civilization, every king and peasant, every young couple in love, every mother and father, hopeful child, inventor and explorer, every teacher of morals, every corrupt politician, every "superstar," every "supreme leader," every saint and sinner in the history of our species lived there– on a mote of dust suspended in a sunbeam.</p>
<p>Space, the final frontier. These are the voyages of the starship Enterprise. Its five year mission: to explore strange new worlds, to seek out new life and new civilizations, to boldly go where no man has gone before!</p>
</article>
<article>
<h2>This section is shorter than the Readmore minimum</h2>
<p>Space, the final frontier. These are the voyages of the starship Enterprise. Its five year mission: to explore strange new worlds, to seek out new life and new civilizations, to boldly go where no man has gone before!</p>