multi-clamp
Version:
Simple, efficient and easy-to-use multiline text clamp module. (supports reverse clamp)
104 lines (71 loc) • 3.92 kB
Markdown
Simple, efficient and easy-to-use multiline text clamp module. (supports reverse clamp)
For React? See [react-multi-clamp](https://github.com/jackyr/react-multi-clamp) [](https://www.npmjs.com/package/react-multi-clamp)
- Default multiline text clamp:

- Custom ellipsis:

- Reverse clamp:

[](https://jackyr.github.io/multi-clamp/example/index.html)
Supports IE8+ / Android4+ / IOS6+ / etc. Almost all of the common browsers on PC / mobile device.
You can install multi-clamp from npm:
```sh
npm install multi-clamp --save
```
Import multi-clamp.
```js
import MultiClamp from 'multi-clamp';
```
Or use [MultiClamp.min.js](https://raw.githubusercontent.com/jackyr/multi-clamp/master/MultiClamp.min.js) in browser directly.
```html
<script src="./MultiClamp.min.js"></script>
```
Just new the multi-clamp constructor.
```html
<div id="textContainer">...much...long...text...</div>
<script>
new MultiClamp(document.getElementById('textContainer'), {
ellipsis: '...',
clamp: 3
});
</script>
```
Ellipsis can be simple string, HTML string or element object. default: '...'
The max number of lines to show. It will try to fill up the available space when set to string 'auto', and at this point you should set a static height on the text container element. default: 3
You can clamp the content from back to front, the ellipsis will be in the front. default: false
The default behavior is to split by letters. If you want to split by words, set splitByWords to true. default: false
Multi-clamp will use native css clamp(-webkit-line-clamp) in supported browser when the ellipsis is set to '...'. If you don't want to use css clamp, set disableCssClamp to true. default: false
#### `onClampStart`: function({ needClamp: boolean }): void || false v1.1.0+
This callback function will be executed when clamp starts, and will not be executed when use native css clamp. Clamp will be prevented when return value is false. default: function() {}
This callback function will be executed when clamp ends, and will not be executed when use native css clamp. default: function() {}
Call this method to re-clamp when the text content or style changes
You can change initial options when reloading through the options param, and use the original text to re-clamp when options.useOriginalText set to true. default: { ...initOptions, useOriginalText: false }
- Bugfix when passing element object to option ellipsis. [react-multi-clamp
- Bugfix. [
- Support change initial options and use the original text to re-clamp. [
- Refactoring. [
- Add onClampStart and onClampEnd callback functions. [react-multi-clamp
- Bugfix. [
MIT