阅读更多/更少链接展开和折叠文本 扩展器

  • 源码大小:225.86KB
  • 所需积分:1积分
  • 源码编号:19JP-3677
  • 浏览次数:952次
  • 最后更新:2023年07月10日
  • 所属栏目:文本
本站默认解压密码:19jp.com 或 19jp_com

简介

Expander是一个简单、可自定义的jQuery文本截断插件,允许您快速轻松地向任何内容添加扩展和折叠功能。

当文本折叠时,“阅读更多”链接将变成一个可点击的锚标记。相反,当文本展开时,无读链接将变成一个锚标记。

这个插件使用jQuery的幻灯片和淡入淡出转换,可以支持内联和块级元素。

如何使用它:

1.在最新的jQuery库之后的某个地方添加jQuery Expander插件。

  1. <script src="/path/to/cdn/jquery.min.js"></script>
  2. <script src="/path/to/jquery.expander.min.js"></script>

2.调用targer元素上的函数。默认情况下,插件会自动折叠超过100个字符的文本。

  1. <p class="demo">
  2. Your Text Here
  3. </p>
  1. $(function(){
  2. $('.demo').expander();
  3. });

3.覆盖默认的字符限制。默认值:100。

  1. $('.demo').expander({
  2. slicePoint: 50,
  3. });

4.确定是否显示单词计数器,该计数器显示“阅读更多”链接后面的单词数。默认值:false。

  1. $('.demo').expander({
  2. showWordCount: true,
  3. wordCountText: ' ({{count}} words)',
  4. });

5.自定义多读和少读链接。

  1. $('.demo').expander({
  2. expandText: 'read more',
  3. expandPrefix: '&hellip; ',
  4. moreLinkClass: 'more-link',
  5. userCollapseText: 'read less',
  6. userCollapsePrefix: ' ',
  7. lessLinkClass: 'less-link',
  8. });

6.设置展开和折叠内容时的过渡。

  1. $('.demo').expander({
  2. // or 'fadeIn'
  3. expandEffect: 'slideDown',
  4. expandSpeed: 250,
  5. // or 'fadeOut'
  6. collapseEffect: 'slideUp',
  7. collapseSpeed: 200,
  8. });

7.更多配置选项。

  1. $('.demo').expander({
  2.  
  3. // whether to keep the last word of the summary whole (true) or let it slice in the middle of a word (false)
  4. preserveWords: true,
  5.  
  6. // whether to normalize the whitespace in the data to display (true) or not (false)
  7. normalizeWhitespace: true,
  8.  
  9. // text to include between summary and detail. Default ' ' prevents appearance of
  10. // collapsing two words into one.
  11. // Was hard-coded in script; now exposed as an option to fix issue #106.
  12. detailPrefix: ' ',
  13.  
  14. // a threshold of sorts for whether to initially hide/collapse part of the element's contents.
  15. // If after slicing the contents in two there are fewer words in the second part than
  16. // the value set by widow, we won't bother hiding/collapsing anything.
  17. widow: 4,
  18.  
  19. expandAfterSummary: false,
  20.  
  21. // Possible word endings to test against for when preserveWords: true
  22. wordEnd: /(&(?:[^;]+;)?|[0-9a-zA-Z\u00C0-\u0100]+|[^\u0000-\u007F]+)$/,
  23.  
  24. // class names for summary element and detail element
  25. summaryClass: 'summary',
  26. detailClass: 'details',
  27.  
  28. // number of milliseconds after text has been expanded at which to collapse the text again.
  29. // when 0, no auto-collapsing
  30. collapseTimer: 0,
  31.  
  32. // allow the user to re-collapse the expanded text.
  33. userCollapse: true,
  34. });

8.回调函数。

  1. $('.demo').expander({
  2.  
  3. onSlice: null, // function() {}
  4. beforeExpand: null, // function() {},
  5. afterExpand: null, // function() {},
  6. onCollapse: null, // function(byUser) {}
  7. afterCollapse: null // function() {}
  8. });

预览截图