English 中文(简体)
jQuery expander with max-lines property
原标题:

I really thinks jQuery Expander plugin is great but it can t do satisfy my needs. It cuts text and put a "read more" button after the text which expands the text. But it only cuts when the text length is more than a specified value. But what if the text is:

Some text:




Blah blah

The text use as much space as a text with many characters but the Expander will not cut it off. I want to have a max lines property so I can restrict both on text length and max lines. Any plugin suggestions?

最佳回答

I have constructed this jQuery which is partly taken from fudgey s link. Unfortunately, it uses px instead of em because I need to compare height of the div with .height(). It could also be prettier to make it to a function but it works :)

$(document).ready(function () {
    var maxlines = 12;
    var lineheight = 15; // line height in  px 
    var maxheight = (maxlines * lineheight);
    var allowedExtraLines = 3;
    var showText = "Læs mere...";
    var hideText = "Skjul tekst...";

    $( .Truncate ).each(function () {
        var text = $(this);
        if (text.height() > maxheight + allowedExtraLines * lineheight) {
            text.css({  overflow :  hidden ,  line-height : lineheight +  px ,  height : maxheight +  px  });

            var link = $( <a href="#">  + showText +  </a> );
            link.click(function (event) {
                event.preventDefault();

                if (text.css( height ) ==  auto ) {
                    $(this).html(showText);
                    text.css( height , maxheight +  px );
                } else {
                    //$(this).remove();
                    $(this).html(hideText);
                    text.css( height ,  auto );
                }
            });

            var linkDiv = $( <div></div> );
            linkDiv.append(link);

            $(this).after(linkDiv);
        }
    });
});
问题回答

暂无回答




相关问题
Which non-bold UTF-8 iPhone SDK font can I use here?

Problem: I create a UILabel with this font: label.font = [UIFont systemFontOfSize:15.0f]; Now this system font is bold, but I want to display pretty small text, and the text may also contain very ...

Limiting file upload type

Simple question. Is there a way to only allow txt files upon uploading? I ve looked around and all I find is text/php, which allows PHP. $uploaded_type=="text/php

Extract everything from PDF [closed]

Looking for solution to extract content from a PDF file (using console tool or a library). It will be used on server to produce on-line e-books from uploaded PDF files. Need to extract following ...

Flash & external form fields

Does anybody know if this is possible? I am trying to create a flash movie that will show / preview what I am typing into a field in a normal HTML form. The call to update the flash movie would most ...

Avoiding escape sequence processing in ActionScript?

I need to mimic C# functionality of the @ symbol when it precedes a string. @"C:AFilePath" for example What is the best way to do this? Also there are some sites that will escape larger ...

iPhone: Draw rotated text?

I want to draw some text in a view, rotated 90°. I m pretty new to iPhone development, and poking around the web reveals a number of different solutions. I ve tried a few and usually end up with my ...

Numbering in jQuery

How could I change the text below so that the text within it has a number appended to it. <div class="right">This is some text</div> <div class="right">This is some text</div>...

热门标签