Client throwing in far too many content? This will automatically abbreviate too-lengthy content with - read more - tags that activate on a mouseover. Then you can click to hide them again. It’ll try and break things at the nearest full stop, (”.”), or the nearest space if it can’t find a period. Hope someone finds this useful - you can see it in action over here.
/*
<ul class="accordion">
<li>Your content here.</li>
</ul>
*/
$(document).ready(function(){
//Snippet settings
var max_length = 110; // Maximum amount of characters to display
var margin_of_error = 80; // If your break point is closer than this value to the end of the string, it won't bother abbreviating it.
var read_more = "( Read More )"; // Message
var read_less = "( Click to hide )"; // Message
$(".accordion li").each(function(){
var length = $(this).html().length;
var text = $(this).html();
if (length >= max_length) {
var point = text.substr(max_length, length).indexOf(".");
if (point == 0) {
point = text.substr(max_length, length).indexOf(" ");
}
if (point + max_length + margin_of_error >= length) {
return; //too few characters to make a difference? Abbreviate.
}
var cutpoint = max_length + point + 1;
var newtext = text.substr(0, cutpoint);
var hiddentext = text.substr(cutpoint, length);
$(this).empty().append(newtext);
$(this).append("<span class=\"hidden\">" + hiddentext + "</span>");
$(this).append("<span class=\"read_more\">" + read_more + "</span>");
}
});
//On mouseover, expand.
$(".read_more").mouseover(function() {
if ($(this).prev(".hidden").css("display") == 'none')
{
if ( $(this).text() == read_more ) {
$(this).text(read_less);
}
else {
$(this).text(read_more);
}
$(this).prev(".hidden").slideDown(400);
}
});
//On mouseclick, hide.
$(".read_more").click(function() {
if ( $(this).text() == read_more ) {
$(this).text(read_less);
}
else {
$(this).text(read_more);
}
$(this).prev(".hidden").slideUp(400);
});
});
|
September 14, 2007
No Comments »
No comments yet.
RSS feed for comments on this post. TrackBack URI
Leave a comment
All drupal-related paintings are licensed under a Creative Commons Attribution-Noncommercial 2.5 Canada License.
Everything else, all rights reserved, so hands off, you! :D

