Last active
May 29, 2016 04:58
-
-
Save nishinoshake/ed2a6cff6607db32b576 to your computer and use it in GitHub Desktop.
行数制限 | 処理がめちゃくちゃ重いので使わないほうがいい
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
restrictRow('.restrict-row', 3, '...'); | |
function restrictRow(elm, row, postfix_arg) { | |
var target = { | |
$elm : $(elm), | |
html : $(elm).html() | |
}, | |
clone = { | |
$elm : target.$elm.clone(), | |
height : 0 | |
}, | |
postfix = postfix_arg || '...', | |
tmp = ''; | |
clone.$elm.css({ | |
opacity : '0', | |
position : 'absolute', | |
overflow : 'visible'} | |
).width(target.$elm.width()).height('auto'); | |
target.$elm.after(clone.$elm); | |
for (var i=0; i<row; i++) tmp += "ぬ<br>"; | |
clone.$elm.html(tmp); | |
clone.height = clone.$elm.height(); | |
clone.$elm.remove(); | |
while((target.html.length > 0) && (target.$elm.height() > clone.height)) { | |
target.html = target.html.substr(0, target.html.length - 1); | |
target.$elm.html(target.html + postfix); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment