Browse files

some fixes for height calculations

  • Loading branch information...
1 parent fa42e91 commit 9999f87c9fa58a00656fa26d32d6837dc7d13770 @vijedi committed Apr 18, 2012
Showing with 19 additions and 16 deletions.
  1. +19 −16 jquery.lineellipsis.js
View
35 jquery.lineellipsis.js
@@ -1,26 +1,29 @@
(function($) {
- $.fn.lineEllipsis = function() {
+ $.fn.lineEllipsis = function() {
return this.each(function() {
var el = $(this);
var md = el.metadata();
if(null != md && md.numLines && el.height() > 0) {
- if(el.css('display') == 'none') {
- el.css('visibility', 'hidden').css('display', 'block');
- }
- var container = el;
- if(md.child) {
- container = el.find(md.child);
- }
+ if(el.css('display') == 'none') {
+ el.css('visibility', 'hidden').css('display', 'block');
+ }
+ var container = el;
+ if(md.child) {
+ container = el.find(md.child);
+ }
- var tmp = $('<span style="visibility: hidden;">WXinabcdy</span>').appendTo(container);
- var charWidth = Math.ceil(tmp.width() / 9);
- var charHeight = tmp.height();
- tmp.remove();
+ var tmp = $('<span style="visibility: hidden;">WXinabcdy</span>').appendTo(container);
+ // Get the width by considering it an inline element -- used for optimizing text sizes
+ var charWidth = Math.ceil(tmp.width() / 9);
+ // Get the height by turning it into a block
+ tmp.css('display', 'block');
+ var charHeight = tmp.height();
+ tmp.remove();
- var text = container.html();
+ var text = container.html();
- var t = $(container[0].cloneNode(true))
+ var t = $(container[0].cloneNode(true))
.css('visibility', 'hidden')
.css('position', 'relative')
.css('overflow', 'visible')
@@ -30,10 +33,10 @@
container.after(t);
- var numLines = md.numLines;
+ var numLines = md.numLines;
var height = numLines * charHeight;
var charsPerLine = Math.ceil(container.width() / charWidth);
- text = text.substr(0, numLines * (charsPerLine) * 1.6);
+ text = text.substr(0, numLines * (charsPerLine) * 1.6); // 1.6 is an arbitrary constant
var times = 0;
while (text.length > 0 && (t.height() > height)) {
text = text.substr(0, text.length - 1);

0 comments on commit 9999f87

Please sign in to comment.