еще парочку подкину идей для сжатия кода.
jQuery.fn.correctLines = function( maxLine, moreText ) {
var self = this[ 0 ], temp, classes, i, lineHeight,
*!*
$ = jQuery;
*/!*
.....
lineHeight.toString().indexOf("px") > 0
~(lineHeight+'').indexOf("px")
....
clone.html('');
jQuery( document.body ).append( clone );
jQuery( document.body ).append( clone.empty() );
....
for( i = 0; i < txt.length; i++ ) {
clone[0].innerHTML += txt[ i ] + ' ';
if ( clone[0].offsetHeight > lastHeight ) {
if ( lastHeight == 0 ) {
height = clone[0].offsetHeight;
}
startWord = endWord + 1;
endWord = i - 1;
lines++;
lastHeight = clone[0].offsetHeight;
}
if ( lines > maxLine ) {
break;
}
}
jQuery.each :
Цитата:
|
We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
|
$.each(txt, function(i, a) {
clone[0].innerHTML += a + ' ';
if (clone[0].offsetHeight > lastHeight) {
if (lastHeight == 0) {
height = clone[0].offsetHeight;
}
startWord = endWord + 1;
endWord = i - 1;
lastHeight = clone[0].offsetHeight;
}
if ( ++lines > maxLine) return false;
});
...
this.css({
*!*
height: lines > maxLine ? (lineHeight * maxLine) + "px" *!*: "auto"*/!*,
*/!*
overflowY: 'hidden'
});
?????
.....
for( i = startWord; i <= endWord; i++ ) {
part.push( txt[ i ] );
}
for( i = part.length - 1; i >= 0; i-- ) {
clone.html( part.join(' ') + moreText );
if ( clone[0].offsetHeight > height ) {
part.splice( i, 1 );
endWord--;
} else {
break;
}
}
не удумал, как заменить, но повторябщиеся операции всегда надо заменять