Created
May 23, 2013 10:49
-
-
Save biruwon/5635241 to your computer and use it in GitHub Desktop.
jQuery truncate
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
<script> | |
(function($){ | |
$.fn.truncate = function(options) { | |
var defaults = { | |
length: 330, | |
minTrail: 0, | |
ellipsisText: "..." | |
}; | |
var options = $.extend(defaults, options); | |
return this.each(function() { | |
obj = $(this); | |
var body = obj.html(); | |
if(body.length > options.length + options.minTrail) { | |
var splitLocation = body.indexOf(' ', options.length); | |
if(splitLocation != -1) { | |
// truncate tip | |
var splitLocation = body.indexOf(' ', options.length); | |
var str1 = body.substring(0, splitLocation); | |
var str2 = body.substring(splitLocation, body.length - 1); | |
obj.html(str1 + '<span class="truncate_ellipsis">' + options.ellipsisText + | |
'</span>' + '<span class="truncate_more">' + str2 + '</span>'); | |
obj.find('.truncate_more').css("display", "none"); | |
} | |
} | |
}); | |
}; | |
})(jQuery); | |
</script> | |
<script> | |
//$(function() { | |
$('[class*=truncate_]').each(function(){ | |
var lengthClass = $(this).attr('class').split('truncate_').pop(); | |
$(this).truncate({ | |
length: parseInt(lengthClass) | |
}); | |
}); | |
//}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment