简体   繁体   English

如何在JavaScript的第二行中用省略号分隔文本?

[英]How to break text with ellipsis on the second line in JavaScript?

I have a post title like this: 我有这样的职位:

 h2 { width: 400px; } 
 <h2>How SEO Optimization Helps Your Website to Become on First Page of Search Engine Result</h2> 

I want to make it appear like this: 我想让它看起来像这样:

  h2 { width: 400px; } 
 <h2>How SEO Optimization Helps Your Website to Become on First Page of...</h2> 

How do I do that in JavaScript or even in JQuery? 如何在JavaScript甚至JQuery中做到这一点?

I want to make my post title get hidden by ellipsis after the second line. 我想让我的帖子标题在第二行之后用省略号隐藏。

Thank you! 谢谢!

With webkit-line-clamp and webkit-box-orient like this: 使用webkit-line-clampwebkit-box-orient这样:

 h2 { width: 400px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; line-height: 21px; max-height:45px; -webkit-line-clamp: 2; -webkit-box-orient: vertical; } 
 <h2>How SEO Optimization Helps Your Website to Become on First Page of Search Engine Result</h2> 

Because firefox not supporting webkit-line-clamp (then doesn't show ellipsis), we can do some tricks with ::after selector ( without text-overflow: ellipsis; and -webkit-line-clamp: 2; ) , something like this: 因为firefox不支持webkit-line-clamp (然后不显示省略号),所以我们可以使用::after选择器(不带text-overflow: ellipsis;-webkit-line-clamp: 2;做一些技巧,就像这个:

For Firefox (also for IE or other browser): 对于Firefox(也适用于IE或其他浏览器):

 h2 { position:relative; width: 400px; overflow: hidden; display: -webkit-box; line-height: 21px; max-height:44px; -webkit-box-orient: vertical; -moz-box-orient: vertical; } h2::after { letter-spacing: .10em; content:"..."; position:absolute; bottom:0; right:0; padding:0 10px 2px 45px; } 
 <h2 class="line-clamp">How SEO Optimization Helps Your Website to Become on First Page of Search Engine Result</h2> 

This could be a whole lot more efficient than it is, but you can get the general idea. 这可能比实际效率要高得多,但是您可以了解一般想法。 This method "feels" for the correct height by adding and removing nodes and testing the height of the block. 通过添加和删除节点并测试块的高度,此方法“感觉”到正确的高度。

 var headings = document.querySelectorAll('h2'); headings.forEach(function(el){ var originalNodes = document.createDocumentFragment(); while (el.firstChild) { var words = el.removeChild(el.firstChild); words.textContent.match(/\\s?\\w+\\s?/g).forEach(function(word){ originalNodes.appendChild(document.createTextNode(word)); }); } el.appendChild(document.createTextNode(String.fromCharCode(8230))); var currentHeight = el.getBoundingClientRect().height; lines = 0; while(originalNodes.childNodes.length > 0 && lines < 2) { el.insertBefore(originalNodes.removeChild(originalNodes.firstChild), el.lastChild); if(el.getBoundingClientRect().height > currentHeight) { currentHeight = el.getBoundingClientRect().height; lines++; } } if(lines === 2) { el.removeChild(el.lastChild.previousSibling); el.lastChild.previousSibling.textContent = el.lastChild.previousSibling.textContent.trim(); } else { el.removeChild(el.lastChild); } }); 
 h2 { width: 400px; } 
 <h2>How SEO Optimization Helps Your Website to Become on First Page of Search Engine Result</h2> <h2>How SEO Optimization Helps Your Website to Become on First Page of Search</h2> <h2>How SEO Optimization Helps Your Website to Become on Testing length</h2> <h2>How</h2> 

You can use the canvas 2D rendering context to measure the text and determine how much text fits on two lines: 您可以使用canvas 2D渲染上下文来测量文本并确定两行可以容纳多少文本:

 var h2 = document.querySelector("h2"); var h2Width = h2.getClientRects()[0].width; var ctx = document.createElement("canvas").getContext("2d"); ctx.font = "bold 24px Arial"; var words = h2.textContent.split(" "); var lineWhichFits = []; var linesUsed = 0; for(var i = 0;i < words.length;i++){ lineWhichFits.push(words[i]); if(ctx.measureText(lineWhichFits.join(" ")).width > h2Width){ linesUsed++; if(linesUsed == 2){ words.splice(i); h2.textContent = words.join(" ") + "..."; break; } lineWhichFits = [lineWhichFits.pop()]; } } 
 h2 { width: 400px; font: bold 24px Arial; } 
 <h2>A title which has lots of text which means it will stretch over a big area, perfect for this demonstration because obvious reasons</h2> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM