Shave

一个用于截断HTML元素中的文本的现代JavaScript插件。(A modern javascript plugin for truncating text within an html element.)

Github星跟踪图

Shave是一个零依赖性JavaScript插件,它截断多行文本,以适应基于set max-height的html元素。 然后将原始文本字符串的diff存储在可见文本后面的隐藏的<span>元素中。 这意味着原始文本保持原样!

与其他截断插件相比,Shave特点如下:
  • 在截断后保留原始文本
  • 不需要其他库
  • 只需要选择器和最大高度
  • 非常轻巧 - 〜1.5kb。不起眼
  • 允许自定义省略号字符串和类名,但不会过于复杂
  • 快速,能够快速截断大量元素中的文本
  • 加性效应。 它将与其他JavaScript库一起完美运行,并且可以轻松地构建更多的截断功能。
  • 支持非间隔语言(Non-ascii)。

概览

名称与所有者dollarshaveclub/shave
主编程语言JavaScript
编程语言JavaScript (语言数: 2)
平台
许可证MIT License
发布数49
最新版本名称2.5.7 (发布于 )
第一版名称v0.0.2 (发布于 2016-09-12 01:39:25)
创建于2016-10-19 21:54:46
推送于2020-10-27 20:20:25
最后一次提交2020-03-26 11:38:39
星数2.1k
关注者数61
派生数121
提交数185
已启用问题?
问题数66
打开的问题数0
拉请求数100
打开的拉请求数2
关闭的拉请求数28
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?

Shave is a zero dependency javascript plugin that truncates multi-line text to fit within an html element based on a set pixel number max-height. It then stores the diff of the original text string in a hidden <span> element following the visible text. This means the original text remains intact!

Shave, compared to other truncation plugins:

  • maintains the original text after truncation.
  • does not require other libraries
  • only requires a selector and a max height
  • is very lightweight; ~1.5kb unminified
  • allows for custom ellipsis strings and class names but doesn't over complicate
  • is fast and capable of truncating text within lots of elements quickly
  • is additive. It will play nice with other javascript libraries and more truncation features can easily be built with it.
  • supports non-spaced languages (Non-ascii).

Installing from a package manager

npm


npm install shave --save

bower


bower install shave --save

yarn


yarn add shave

Usage

Add dist/shave.js to your html

  • Or, dist/jquery.shave.js for jQuery/Zepto as of Shave >= v2.

Or as a module


import shave from 'shave';

Syntax

Basic setup


shave('selector', maxheight);
// shave('.shave-selector', 0) for example

Shave also provided options only to overwrite what it uses.

If you'd like have custom class names and not use .js-shave:


shave('selector', maxheight, { classname: 'classname' });

Or if you'd like to have custom characters (instead of the standard ellipsis):


shave('selector', maxheight, { character: '✁' });

Or both:


shave('selector', maxheight, { classname: 'classname', character: '✁' });

Without spaces:


shave('selector', maxheight, { spaces: false });


You can also use shave as a jQuery or Zepto plugin. As of Shave >= v2, use dist/jquery.shave.js for jQuery/Zepto.


$('selector').shave(maxheight);

And here's a jQuery/Zepto example with custom options:


$('selector').shave(maxheight, { classname: 'your-css-class', character: '✁'  });

If you're using a non-spaced language, you can support shave by setting an option spaces to false.


$('selector').shave(maxheight, { classname: 'your-css-class', character: '✁', spaces: false });

Examples

Codepen example with plain javascript.

Codepen example with jQuery.

Codepen example with a non-spaced language.

Notes

text-overflow: ellipsis is the way to go when truncating text to a single line. Shave does something very similar to text-overflow: ellipsis but for multiple lines when line-clamp is not supported. Shave bypasses being a line-clamp polyfill by only accepting a max-height number. This keeps shave a fast and light weight utility.

Shave implements a binary search to truncate text in the most optimal way possible.

Shave is meant to truncate text within a selected html element. This means it will overwrite html within an html element with just the text within the selected element.

Here are some super basic examples of shave with window resize and click events. ?

Shave works in all modern browsers and was tested in some not so modern browsers (like Internet Explorer 8) - it works there too. ?


Created and maintained by Jeff Wainwright with Dollar Shave Club Engineering.

去到顶部