javascript “阅读更多”链接以显示全文

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript “阅读更多”链接以显示全文相关的知识,希望对你有一定的参考价值。

export { Readmore };

const $ = jQuery.noConflict();

const Readmore = {

    // check to see if current panel is open or not
    init(elClass = '.js-readmore') {
        
        // add required styles
        $(elClass).css({
            'overflow': 'hidden',
            'transition': 'height .75s ease'
        });
    

        // get the height in PX of the first element within the elClass (usually .js-readmore)
        const closedheight = $(elClass + " > *:first").outerHeight(true);        
        const openheight = $(elClass).outerHeight(true);
        
        
        // wrap elClass
        $(elClass).wrap('<div class="js-readmore-wrap"></div>');

        // set to height of first paragraph
        $(elClass).css('height', closedheight);


        // add class to set status to closed
        $(elClass).addClass('js-readmore--closed');

        // add show more link
        $(elClass).after('<span class="read-more-link">Read More</span>');

        // create Read More click handler
        $('.js-readmore-wrap').on('click', '.read-more-link', function () {
            
            // select the readmore class that was clicked
            const thisClickedReadMore = $(this).siblings(elClass);

            if ( thisClickedReadMore.hasClass('js-readmore--closed') ){

                // add class to set status to open
                $(thisClickedReadMore).addClass('js-readmore--open');

                // set the height
                $(thisClickedReadMore).css('height', openheight);

                // remove closed class
                $(thisClickedReadMore).removeClass('js-readmore--closed');

                // remove the read more link
                $(this).remove(".read-more-link");
            }
            
        });


    }

}

以上是关于javascript “阅读更多”链接以显示全文的主要内容,如果未能解决你的问题,请参考以下文章

如何在 wordpress 的主页上显示裁剪的图像?

Readmore.js 实现

阅读更多 UIButton 上的单元格高度增加问题

展开 div 以“阅读更多”

VueJS - 我无法在达到所选限制数据之前或之后隐藏阅读更多按钮以在 vuejs 中显示

如何使用 TTTAttributedLabel 添加“阅读更多”