Skip to content

Instantly share code, notes, and snippets.

@plutochill
Created April 17, 2014 05:51
Show Gist options
  • Save plutochill/10956060 to your computer and use it in GitHub Desktop.
Save plutochill/10956060 to your computer and use it in GitHub Desktop.
多行文本溢出显示省略号(...)的方法
//jquery
//从后向前逐个删除末尾字符,直至元素的高度小于父元素高度
$(".figcaption").each(function(i){
var divH = $(this).height();
var $p = $("p", $(this)).eq(0);
while ($p.outerHeight() > divH) {
$p.text($p.text().replace(/(\s)*([a-zA-Z0-9]+|\W)(\.\.\.)?$/, "..."));
};
});
//css
p {
overflow : hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment