I want to take a text on a webpage and measure it's height in lines, regardless of any other parameter,
so if I shrink the text width and get more lines, i will get the new line count.
Any ideas how to do that using JS and jQuery?
Also, what kind of line numbering techniques are common?
Almost same idea as Ghommey's comment, but with line-height instead of height.
Fiddle here: http://jsfiddle.net/k_rma/vbCJm/
Here is a small jQuery function doing it:
$.fn.numLines = function() {
var tmp = $('<div style="visibility:hidden;">x</div>').appendTo(document.body);
var lines = this.height() / tmp.height();
tmp.remove();
return lines;
};
You simply call $('#someElement').numLines() to retrieve the number of lines.
Here's a demo: http://jsfiddle.net/ThiefMaster/sHzMP/
This will give total number of line in given text
text.split("\n").length
Related
Title. I've been looking around for a while in Google for this but I only found a plugin that sets the LineHeight (https://ckeditor.com/addon/lineheight), and doesn't seem to allow to calculate it.
Is it possible? How would I calculate it?
This addon use line-height css property, which can be specified in %
e.g. you can try
config.line_height="10%;20%;30%" ;
If it's not solved your problem, can you clarify, please, - calculate line-height based on that and what describe please "the first line" ?
UPDATE:
var firstElement = CKEDITOR.instances["textarea"].document.getBody().getFirst();
var firstLineHeight = firstElement.$.offsetHeight;
I have seen this done a few times before. I can't remember the other sites, but if you know of others I'd like to see. (https://gumroad.com/)
Basically, when they do something like:
Share your 'songs' directly to your 'fans'. And the words in the quotes keep getting replaced.
I was wondering if there is a JS/Jquery library to help with that. Or otherwise I'd like some other websites that do it so I can get ideas!
Thanks.
I implemented something similar (without animations) using jQuery.
The code basically cycles data from an array every 2000ms, and adds it to a span.
Live Demo - http://jsfiddle.net/Dogbert/vF4rj/3/
JS:
var options = [
["music", "listeners"],
["software", "users"],
["films", "viewers"],
["comics", "readers"]
];
var interval = 2000;
var holder1 = $(".holder-1"), holder2 = $(".holder-2");
var currentIndex = 0;
function doIt() {
holder1.html(options[currentIndex][0]);
holder2.html(options[currentIndex][1]);
currentIndex = (currentIndex + 1) % options.length;
setTimeout(doIt, interval);
}
doIt();
HTML:
<h2>
Sell
<span class="holder-1"></span>
directly to your
<span class="holder-2"></span>
</h2>
If you are interested in their approach, it seems like this is what they are doing:
They have the header text in their code twice. One is what you see being animated. The other is hidden by setting its opacity to 0. This hidden version is what will be used to do any calculations on the displayed one.
The hidden version has its text updated by inserting new text into placeholder span tags. The displayed version has placeholders in that spot that are empty spaces, and their width is animated by using the new width from the hidden version.
The hidden version is also used to determine the positioning of the new text relative to the rest of the sentence, and based on that information, an HTML element containing that text is animated to fall in perfectly where it should.
A pretty clever approach.
What makes this work for Gumroad is the slick animation so I feel answers are incomplete without this essential element.
That being said, the d3 javascript library (d3js.org) has a very good implementation of the base functionality plus support for transitions. The key code is the following which allows you to easily specify the type of css transform you want to be applied (see the last line):
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(32," + (height / 2) + ")");
A full example can be viewed here: http://bl.ocks.org/mbostock/3808234
It's not the first time I'm bumping into this problem and never have the opportunity to figure out something right.
So here's the problem:
I have 2 divs. Each of them have a title in them. Depending on the length of the title, the title may have to fit on 2 lines or not. All of this is generated from a CMS, so no way of telling if one of them will have 2 lines or one at a given time (so I can't just separate them with a class if they have 1 line or 2)
Here's the juicy part: I want the title in those two divs centered vertically. Line height won't do it because theres always the possibility of it being on 1 or 2 lines, and display:table-cell + vertical-align:middle is not compatible with IE7.
So is there a way to vertically center those titles and that will work in every browser?
Edit: Heres a photo of the situation I try to explain:
Edit #2: Here's a code sample recreating my example. We want to get them both centered. http://jsfiddle.net/m7bLj/
Edit #3:
After the answer of #mikhailvs, I came up with this code, that worked.
$(document).ready(function(){
$(".container-div").each(function(){
$(this).children('div').css('top', String($(this).height() / 2 - $(this).children('div').height() / 2) + 'px');
})
})
If you are willing to use jQuery, you can do something like this:
var div1onLoadCallback = function () {
var w = $(window);
var div1 = $('#div1');
div1.css('top', String(w.height() / 2 - div1.height() / 2) + 'px');
}
Then declare your div like <div id="div1" onload="div1onLoadCallback()"></div>
and similarly for the second div. Of coarse, you would need to modify the css position attribute for the titles for it to work (such as position: absolute).
In a generated html page, we have a fixed size area (lets say 200 x 300) in which we need to fit in as much text as possible (like a regular paragraph of text), and if it doesn't fit, remove an appropriate number of characters and append "..." to the end.
The text is NOT in a fixed sized font, and although we are using one specific font for this text, a "generic" solution would obviously be preferred.
This looked interesting, but I'm thinking it would be very slow with this function being called for several items on a page - http://bytes.com/topic/javascript/answers/93847-neatly-truncating-text-fit-physical-dimension
The solution can use an intermix of html, css, js, and php as needed.
Suggestions on approaches are more than welcome!
I'd say that the solution you found is the best. It is, for instance, used for this jQuery plugin which autoresizes textareas as you enter text into it. I took the concept and rewrote it with jQuery for this simple test here: http://jsfiddle.net/ZDr5K/
var para = $('#para');
var height = 200;
while(para.height() >= height){
var text = para.text();
para.text(text.substring(0, text.length - 4) + '...');
}
Possible improvements would include right trimming and removing the period if the last character is a full stop. Removing word by word would also be more readable/slightly faster.
As for the function running multiple times, that would be unavoidable. The only thing you can really do with CSS here is to use :after to append the ellipses, but even that should be avoided for cross-compatibility problems.
Set the element dimensions via CSS and its overflow to "hidden".
Then, find out with this function, if the element's content is overflowing (via):
// Determines if the passed element is overflowing its bounds,
// either vertically or horizontally.
// Will temporarily modify the "overflow" style to detect this
// if necessary.
function checkOverflow(el)
{
var curOverflow = el.style.overflow;
if ( !curOverflow || curOverflow === "visible" )
el.style.overflow = "hidden";
var isOverflowing = el.clientWidth < el.scrollWidth
|| el.clientHeight < el.scrollHeight;
el.style.overflow = curOverflow;
return isOverflowing;
}
Now, in a loop remove text and check until it is not overflowing anymore. Append an ellipsis character (String.fromCharCode(8230)) to the end, but only if it was overflowing.
To avoid any flickering effects during that operation, you can try working on a hidden copy of the element, but I'm not sure if the browsers do the necessary layout calculations on an element that's not visible. (Can anyone clarify that?)
I need a Jquery script to truncate a text paragraph by line (not by character count).
I would like to achieve an evenly truncated text-block. It should have a "more" and "less" link to expand and shorten the text paragraph. My text paragraph is wrapped in a div with a class, like this:
<div class="content">
<h2>Headline</h2>
<p>The paragraph Text here</p>
</div>
The closest solution i could find on SOF is the one below (but it`s for textarea element and does not work for me):
Limiting number of lines in textarea
Many thanks for any tips.
Ben
For a basic approach, you could take a look at the line-height CSS property and use that in your calculations. Bear in mind that this approach will not account for other inline elements that are larger than that height (e.g. images).
If you want something a bit more advanced, you can get the information about each line using getClientRects() function. This function returns a collection of TextRectangle objects with width, height and offset for each one.
See this answer here for an example (albeit an unrelated goal) of how getClientRects() works.
Update, had a bit of time to come back and update this answer with an actual example. It's basic, but you get the idea:
http://jsbin.com/ukaqu3/2
A couple of pointers:
The collection returned by getClientRects is static, it won't update automatically if the containing element's dimensions change. My example works around this by capturing the window's resize event.
For some strange standards-compliance reason that I'm not understanding, the element you call getClientRects on must be an inline element. In the example I have, I use a container div with the text in another div inside with display: inline.
I made this little jQuery code to allow me truncate text blocks by line (via CSS classes), feel free to use and comment it.
Here is the jsFiddle, which also include truncate functions by char count or word count. You can see that currently, resize the window won't refresh the block display, I'm working on it.
/*
* Truncate a text bloc after x lines
* <p class="t_truncate_l_2">Lorem ipsum magna eiusmod sit labore.</p>
*/
$("*").filter(function () {
return /t_truncate_l_/.test($(this).attr('class'));
}).each(function() {
var el = $(this);
var content = el.text();
var classList = el.attr('class').split(/\s+/);
$.each(classList, function(index, item){
if(/^t_truncate_l_/.test(item)) {
var n = item.substr(13);
var lineHeight = parseInt(el.css('line-height'));
if(lineHeight == 1 || el.css('line-height') == 'normal')
lineHeight = parseInt(el.css('font-size')) * 1.3;
var maxHeight = n * lineHeight;
var truncated = $.trim(content);
var old;
if(el.height() > maxHeight)
truncated += '...';
while(el.height() > maxHeight && old != truncated) {
old = truncated;
truncated = truncated.replace(/\s[^\s]*\.\.\.$/, '...');
el.text(truncated);
}
}
});
});
why not make the p element with overflow: hidden; give fixed line height, caluclate the height of the div so id contains exactly the number of lines you require and the only change the height of the p from javascript.
p{
overflow:hidden;
line-height:13px;
height:26px; /* show only 2 rows */
}
<script type="text/javascript">
function seeMoreRows(){
$(p).height("52px");
}
</script>
I made a small module that works with pure text content, no nested tags and no css-padding on the text-containing element is allowed (but this functionality could easily be added).
The HTML:
<p class="ellipsis" data-ellipsis-max-line-count="3">
Put some multiline text here
</p>
The Javascript/Jquery:
( function() {
$(document).ready(function(){
store_contents();
lazy_update(1000);
});
// Lazy update saves performance for other tasks...
var lazy_update = function(delay) {
window.lazy_update_timeout = setTimeout(function(){
update_ellipsis();
$(window).one('resize', function() {
lazy_update(delay);
});
}, delay);
}
var store_contents = function(){
$('.ellipsis').each(function(){
var p = $(this);
p.data('ellipsis-storage', p.html());
});
}
var update_ellipsis = function(){
$('.ellipsis').each(function(){
var p = $(this);
var max_line_count = p.data('ellipsis-max-line-count');
var line_height = p.html(' ').outerHeight();
var max_height = max_line_count*line_height;
p.html(p.data('ellipsis-storage'));
var p_height = p.outerHeight();
while(p_height > max_height){
var content_arr = p.html().split(' ');
content_arr.pop();
content_arr.pop();
content_arr.push('...');
p.html(content_arr.join(' '));
p_height = p.outerHeight();
}
});
}
} )();
I hope you like it!
If you used a monospaced font, you'd have a shot at this working, as you'd have a good idea how many letters fit onto each line, for an element of a defined width. However, if a word breaks across lines, then this might get tricky..
e: found another question which is basically what you're after - they didn't really have a resolution either, but to my mind, the line-height and element height seems closest.
"How can I count text lines inside a dom element"
tl;dr - set a height on your container div and then use the jQuery dotdotdot plugin
Was about to make #Andy E's awesome example into a plugin, but then realized https://github.com/BeSite/jQuery.dotdotdot could pull this off. Our use case is we want to show one line on desktop widths and two lines on mobile/tablet.
Our CSS will just set the container div to the equivalent of one or two line-height's accordingly and then the dotdotdot plugin appears to handle the rest.