扩展Read more / read less jQuery脚本

时间:2012-12-13 14:07:50

标签: javascript jquery

我有一个很棒的脚本,使用jQuery来显示一小部分内容&允许用户阅读更多/阅读更少的内容。 这是我目前的小提琴http://jsfiddle.net/Tqwdh/1/

摘要:点击图片后,需要显示其他文字。

我很想知道如何更新此脚本以允许用户单击关联的图像,它将显示更多文本(以及保持文本链接到位)。

有人能告诉我如何实现这个目标吗?

以下是我的示例 HTML

<article id="post-5" >

            <div class="more-less">    
                <div class="more-block">
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed diam purus, lacinia eget placerat sit amet, bibendum nec nisl. Curabitur a mattis ipsum. Praesent quis nisi in purus malesuada rhoncus. Pellentesque ut quam eget libero congue lobortis. Nunc sed quam ac erat lobortis eleifend. Donec elementum sodales cursus. Aliquam porttitor massa nisi, in laoreet turpis. Sed consequat condimentum lorem ut dignissim. Sed hendrerit mauris ut massa fermentum laoreet. Pellentesque a leo vitae enim dictum lobortis. Praesent commodo feugiat velit iaculis malesuada.</p>
                    <p>Phasellus id elit ac lacus faucibus ullamcorper. Etiam ullamcorper pretium tellus, ut pharetra ante pulvinar vel. Sed neque diam, semper vel rhoncus non, congue ut sapien. Integer a mi eget libero elementum lobortis. Donec hendrerit egestas ligula sit amet eleifend. Curabitur pharetra venenatis tempor. Quisque pulvinar malesuada justo, ut euismod arcu pharetra vitae. Cras lobortis, ligula id euismod euismod, ipsum risus varius urna, faucibus gravida lectus mi nec nulla. Fusce eleifend fringilla nibh ut vulputate. Vivamus sagittis leo metus. Etiam facilisis convallis lacus adipiscing hendrerit. Duis ultricies euismod libero, nec blandit est semper a. Phasellus sit amet justo sed quam elementum lobortis. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
                </div>
            </div>

            <p>
                <a href="#" title="News Item 1"><img src="http://placehold.it/300x187" alt="News Item 1" width="300" height="187" /></a>
            </p>            

        </article><!-- #post-## -->

和我的 jQuery

$(function(){
        // The height of the content block when it's not expanded
        var adjustheight = 130;
        // The "more" link text
        var moreText = "Click to read more...";
        // The "less" link text
        var lessText = "Click to read less...";
        // Sets the .more-block div to the specified height and hides any content that overflows
        $(".more-less .more-block").css('height', adjustheight).css('overflow', 'hidden');
        // The section added to the bottom of the "more-less" div
        $(".more-less").append('<p style="display:block;margin-top:8px"><a href="#" class="adjust"></a></p>');
        $("a.adjust").text(moreText);
        $(".adjust").toggle(function() {
                $(this).parents("div:first").find(".more-block").css('height', 'auto').css('overflow', 'visible');
                // Hide the [...] when expanded
                $(this).parents("div:first").find("p.continued").css('display', 'none');
                $(this).text(lessText);
            }, function() {
                $(this).parents("div:first").find(".more-block").css('height', adjustheight).css('overflow', 'hidden');
                $(this).parents("div:first").find("p.continued").css('display', 'block');
                $(this).text(moreText);
        });
        });

谢谢: - )

3 个答案:

答案 0 :(得分:2)

你可以像这样添加一个点击处理程序给img: DEMO

$(function(){
    $('img').click(function(){
        $(this).closest('article').find('.adjust').click();
    });
});​

答案 1 :(得分:1)

您需要将逻辑从toggle handler移动到您自己的实现,因为您有多个事件源。

<article id="post-5" >
    <div class="more-less">    
        <div class="more-block">…</div>
    </div>
    <p>
         <a href="#post-5" class="adjust" title="News Item 1">…</a>
    </p>            
</article>
$(function(){
    var adjustheight = 130; // The height of the content block when it's not expanded
    var texts = {
        more: "Click to read more…",
        less: "Click to read less…"
    };
    $("article").each(function() {
        var block = $(".more-block", this).css({height:adjustheight, overflow:'hidden'}),
            cont = $("p.continue", this),
            toggle = $('<a href="#'+this.id+'" class="adjust">').text(texts.more),
            open = false;
        $(".more-less", this).append($('<p style="display:block;margin-top:8px">').append(link));
        $("a.adjust", this).click(function() {
            open = !open;
            block.css("height", open ? "auto" : adjustheight);
            link.text(texts[open ? "less" : "more"]);
            cont[open ? "show" : "hide"]();
        }
    });
});

答案 2 :(得分:1)

只需将“Click to read more”元素的相同行为添加到图像元素即可。

当你设置这样的切换处理程序时:

$(".adjust").toggle(function() {
        $(this).parents("div:first").find(".more-block").css('height', 'auto').css('overflow', 'visible');
        // Hide the [...] when expanded
        $(this).parents("div:first").find("p.continued").css('display', 'none');
        $(this).text(lessText);
    }, function() {
        $(this).parents("div:first").find(".more-block").css('height', adjustheight).css('overflow', 'hidden');
        $(this).parents("div:first").find("p.continued").css('display', 'block');
        $(this).text(moreText);
    });

只需使用jQuery的multiple selector来选择图像元素,如下例所示:

$(".adjust, #img1, #img2").toggle(function() {
        $(this).parents("div:first").find(".more-block").css('height', 'auto').css('overflow', 'visible');
        // Hide the [...] when expanded
        $(this).parents("div:first").find("p.continued").css('display', 'none');
        $(this).text(lessText);
    }, function() {
        $(this).parents("div:first").find(".more-block").css('height', adjustheight).css('overflow', 'hidden');
        $(this).parents("div:first").find("p.continued").css('display', 'block');
        $(this).text(moreText);
    });

Demo

希望它有所帮助。