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.

去到頂部