Giter Site home page Giter Site logo

hagalas / readmore.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jedfoster/readmore.js

0.0 1.0 0.0 443 KB

A lightweight jQuery plugin for collapsing and expanding long blocks of text with "Read more" and "Close" links.

Home Page: http://jedfoster.github.com/Readmore.js/

readmore.js's Introduction

Readmore.js

A smooth, lightweight jQuery plugin for collapsing and expanding long blocks of text with "Read more" and "Close" links.

The required markup for Readmore.js is also extremely lightweight and very simple. No need for complicated sets of divs or hardcoded class names, just call .readmore() on the element containing your block of text and Readmore.js takes care of the rest.

Readmore.js is compatible with all versions of jQuery greater than 1.7.0.

Example

$('article').readmore();

Yes, it's that simple. You can change the speed of the animation, the height of the collapsed block, and the open and close elements.

$('article').readmore({
  speed: 75,
  maxHeight: 500
});

The options:

  • speed: 100 (in milliseconds)
  • maxHeight: 200 (in pixels)
  • heightMargin: 16 (in pixels, avoids collapsing blocks that are only slightly larger than maxHeight)
  • moreLink: '<a href="#">Read more</a>'
  • lessLink: '<a href="#">Close</a>'
  • embedCSS: true (insert required CSS dynamically, set this to false if you include the necessary CSS in a stylesheet)
  • sectionCSS: 'display: block; width: 100%;' (sets the styling of the blocks, ignored if embedCSS is false)
  • startOpen: false (do not immediately truncate, start in the fully opened position)
  • expandedClass: 'readmore-js-expanded' (class added to expanded blocks)
  • collapsedClass: 'readmore-js-collapsed' (class added to collapsed blocks)
  • beforeToggle: function() {} (called after a more or less link is clicked, but before the block is collapsed or expanded)
  • afterToggle: function() {} (called after the block is collapsed or expanded)

If the element has a max-height CSS property, Readmore.js will use that value rather than the value of the maxHeight option.

The callbacks:

The callback functions, beforeToggle() and afterToggle, both receive the same arguments: trigger, element, and expanded.

  • trigger: the "Read more" or "Close" element that was clicked
  • element: the block that is being collapsed or expanded
  • expanded: Boolean; true means the block is expanded

Callback example:

Here's an example of how you could use the afterToggle callback to scroll back to the top of a block when the "Close" link is clicked.

$('article').readmore({
  afterToggle: function(trigger, element, expanded) {
    if(! expanded) { // The "Close" link was clicked
      $('html, body').animate( { scrollTop: element.offset().top }, {duration: 100 } );
    }
  }
});

Recommended CSS:

The intention behind Readmore.js is to use CSS for as much functionality as possible. In particular, "collapsing" is achieved by setting overflow: hidden on the containing block and changing the height property.

By default, Readmore.js inserts the following CSS:

.readmore-js-toggle, .readmore-js-section {
  display: block;
  width: 100%;
}
.readmore-js-section {
  overflow: hidden;
}

You can override the the first set of rules when you set up Readmore.js like so:

$('article').readmore({sectionCSS: 'display: inline-block; width: 50%;'});

If you want to include the necessary styling in your site's stylesheet, you can disable the dynamic embedding by passing embedCSS: false in the options hash.

$('article').readmore({embedCSS: false});

Removing Readmore

You can remove the Readmore functionality like so:

$('article').readmore('destroy');

Or, you can be more surgical by specifying a particular element:

$('article:first').readmore('destroy');

readmore.js's People

Contributors

jedfoster avatar kaaloo avatar kmark avatar nmatpt avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.