html 单击readmore 无效,Readmore.js/demo.html at master · jedfoster/Readmore.js · GitHub

Readmore.js: jQuery plugin for long blocks of text

body { font: 16px/1.5 "Helvetica Neue", Arial, Helvetica, sans-serif; color: #444; }

code { color: #777; font-family: "Source Code Pro", "Menlo", "Courier New", monospace;}

a { color: #178DB1; }

.container { margin: 0 auto; max-width: 960px; }

#info + .readmore-js-toggle { padding-bottom: 1.5em; border-bottom: 1px solid #999; font-weight: bold;}

#demo { padding: 0 10%; }

Readmore.js

A smooth, responsive jQuery plugin for collapsing and expanding long blocks of text with “Read more” and “Close” links.

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 div’s or hardcoded classes, just call .readmore() 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.

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 should work, but are not supported and the experience will not be ideal.

Install

Install Readmore.js with npm:

$ npm install readmore-js

Then include it in your HTML:

<script src="/node_modules/readmore-js/readmore.min.js"></script>

Or, using Webpack or Browserify:

require('readmore-js');

Use

$('article').readmore();

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,

lessLink: '<a href="#">Read less</a>'

});

The options:

  • speed: 100 in milliseconds
  • collapsedHeight: 200 in pixels
  • heightMargin: 16 in pixels, avoids collapsing blocks that are only slightly larger than collapsedHeight
  • 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
  • blockCSS: '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
  • 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
  • blockProcessed: function() {} called once per block during initilization after Readmore.js has processed the block.

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

The callbacks:

The beforeToggle and afterToggle callbacks 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

The blockProcessed callback receives element and collapsable.

  • element: the block that has just been processed
  • collapsable: Boolean; false means the block was shorter than the specified minimum collapsedHeight—the block will not have a "Read more" link
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 } );

}

}

});

Removing Readmore:

You can remove the Readmore.js functionality like so:

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

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

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

Toggling blocks programmatically:

You can toggle a block from code:

$('article:nth-of-type(3)').readmore('toggle');

CSS:

Readmore.js is designed to use CSS for as much functionality as possible: collapsed height can be set in CSS with the max-height property; “collapsing” is achieved by setting overflow: hidden on the containing block and changing the height property; and, finally, the expanding/collapsing animation is done with CSS3 transitions.

By default, Readmore.js inserts the following CSS, in addition to some transition-related rules:

selector + [data-readmore-toggle], selector[data-readmore] {

display: block;

width: 100%;

}

selector would be the element you invoked readmore() on, e.g.: $('selector').readmore()

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

$('article').readmore({blockCSS: '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 setting embedCSS to false:

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

Media queries and other CSS tricks:

If you wanted to set a maxHeight based on lines, you could do so in CSS with something like:

body {

font: 16px/1.5 sans-serif;

}

/* Show only 4 lines in smaller screens */

article {

max-height: 6em; /* (4 * 1.5 = 6) */

}

Then, with a media query you could change the number of lines shown, like so:

/* Show 8 lines on larger screens */

@media screen and (min-width: 640px) {

article {

max-height: 12em;

}

}

Contributing

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.

Pull requests should include the minified script and this readme and the demo HTML should be updated with descriptions of your new feature.

You’ll need NPM:

$ npm install

Which will install the necessary development dependencies. Then, to build the minified script:

$ gulp compress

Demo

Artisanal Narwahls

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.

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!

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.

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!

Portland Leggings

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.

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.

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.

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!

This section is shorter than the Readmore minimum

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!

$('#info').readmore({

moreLink: 'Usage, examples, and options',

collapsedHeight: 384,

afterToggle: function(trigger, element, expanded) {

if(! expanded) { // The "Close" link was clicked

$('html, body').animate({scrollTop: element.offset().top}, {duration: 100});

}

}

});

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值