Git Product home page Git Product logo

clamp-js-main's Introduction

Clamps (ie. cuts off) an HTML element's content by adding ellipsis to it if the content inside is too long.

Installation Instructions

  • Open terminal and move to your project directory and run npm install clamp-js-main
  • You can also install by running git clone [email protected]:jmenglis/clamp-js-main.git and moving the script.js to your project. Then include it in your index.html.

Sample Usage

//Single line
$clamp(myHeader, {clamp: 1});

//Multi-line
$clamp(myHeader, {clamp: 3});

//Auto-clamp based on available height
$clamp(myParagraph, {clamp: 'auto'});

//Auto-clamp based on a fixed element height
$clamp(myParagraph, {clamp: '35px'});

The $clamp method is the primary way of interacting with Clamp.js, and it takes two arguments. The first is the element which should be clamped, and the second is an Object with options in JSON notation.

Options

clamp (Number | String | 'auto'). This controls where and when to clamp the text of an element. Submitting a number controls the number of lines that should be displayed. Second, you can submit a CSS value (in px or em) that controls the height of the element as a String. Finally, you can submit the word 'auto' as a string. Auto will try to fill up the available space with the content and then automatically clamp once content no longer fits. This last option should only be set if a static height is being set on the element elsewhere (such as through CSS) otherwise no clamping will be done.

useNativeClamp (Boolean). Enables or disables using the native -webkit-line-clamp in a supported browser (ie. Webkit). It defaults to true if you're using Webkit, but it can behave wonky sometimes so you can set it to false to use the JavaScript- based solution.

truncationChar (String). The character to insert at the end of the HTML element after truncation is performed. This defaults to an ellipsis (โ€ฆ).

truncationHTML (String). A string of HTML to insert before the truncation character. This is useful if you'd like to add a "Read more" link or some such thing at the end of your clamped node.

splitOnChars (Array). Determines what characters to use to chunk an element into smaller pieces. Version 0.1 of Clamp.js would always remove each individual character to check for fit. With v0.2, you now have an option to pass a list of characters it can use. For example, it you pass an array of ['.', ',', ' '] then it will first remove sentences, then remove comma-phrases, and remove words, and finally remove individual characters to try and find the correct height. This will lead to increased performance and less looping when removing larger pieces of text (such as in paragraphs). The default is set to remove sentences (periods), hypens, en-dashes, em-dashes, and finally words (spaces). Removing by character is always enabled as the last attempt no matter what is submitted in the array.

animate (Boolean). Silly little easter-egg that, when set to true, will animate removing individual characters from the end of the element until the content fits. Defaults to false.

Version

  • v0.11.6 : Adding in import support for es6. Thanks amirgro!
  • v0.11.5 : Fixing issue with children and childNodes on IE and Firefox. Thanks AngKov
  • v0.11.4 : Fix issues with line-height normal in Chrome vs other browsers.
  • v0.11.3 : Removing logging to reduce console pollution. Thanks oliveti!
  • v0.11.2 : Fixing issues with Firefox displaying different information from Chrome.
  • v0.11.1 : Adjusting readme.
  • v0.11.0 : Adjusting git location.
  • v0.10.0 : Adjusting name of repo and pointing package.json to the right location.
  • v0.9.0 : Added changes for support on Firefox and IE.

Original Credit

clamp-js-main's People

Contributors

anzhelikakovalchuk avatar el-teedee avatar jmenglis avatar josephschmitt avatar nedbaldessin avatar xavi160 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

clamp-js-main's Issues

how to change truncation style ?

I want to change the code below(line between 255-259)

sty.overflow = 'hidden';
sty.textOverflow = 'ellipsis';
sty.webkitBoxOrient = 'vertical';
sty.display = '-webkit-box';
sty.webkitLineClamp = clampValue;

to this

element.stylesheet.insertRule(
 '.content::after{' +
 'content: "..."; position: absolute; bottom: 0; right: 0; padding-left: 40px;' +
 'background: -webkit-linear-gradient(left, transparent, #fff 55%);' +
 "background: -o-linear-gradient(right, transparent, #fff 55%);" +
 'background: -moz-linear-gradient(right, transparent, #fff 55%);' +
 '+background: linear-gradient(to right, transparent, #fff 55%);}'
 ,0);

but, it't don't work, and the class '.content::after' don't add to the element,

so, is there a method that I can add custom truncation style more easier?

Thanks a lot for your help!

Does not work with inline elements

Hey mate, it does work but not with display: inline elements. And I can't remove the display: inline what should I do?
.article-card .card-title { padding: 0rem 0.3rem; line-height: 1.56; display: inline; box-decoration-break: clone; -webkit-box-decoration-break: clone; border-radius: 2px; transition: background 0.2s cubic-bezier(0.455, 0.03, 0.515, 0.955), color 0.3s cubic-bezier(0.455, 0.03, 0.515, 0.955); }
capture

Clamping in IE

I'm having an issue with clamping in IE to 2 lines only.
Have the same function with 3 line, and it works perfectly, and it clamps the full component to ellipsis if $clamp(element, { clamp: 2});

Would appreciate any suggestions

Not working In Firefox

I open the example in chrome (Version 58.0.3029.81) and doesn't show the "<h1>" text.
I tested in my own code and set 'clamp: 3' and some items show 3 lines and other shows 4 (this is in firefox)

Truncation Characters not working

Changing Truncation Characters does not current work in clamp-js.

$clamp(paragraph, {
      clamp: 10,
      useNativeClamp: false,
      animate: true
      truncationChar: '!!!'
});

Version: 0.11.4

In Firefox/IE if clamp is set to 2, it still shows an ellipsis on the 2nd line, even if the line was not clamped.

In Chrome, the text looks correct... i.e no ellipsis on the 2nd line of text, as the text is not wrapping to a 3rd line...
http://pasteboard.co/ArBHmdnam.jpg

But in IE/Firefox, even though the 2nd line is nowhere near wrapping, it still shows an ellipsis.
http://pasteboard.co/3DXHJQgQp.jpg

I am using

$('.clamp').each(function (i, obj) { $clamp(obj, { clamp: 2, useNativeClamp: false, truncationChar: '...' }); });

Note : I had to set useNativeClamp to false to prevent another issue in Safari, which added a ... in the middle of the sentence.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.