jquery展开收缩的查看更多自定义折叠特效代码

所属分类: 网页特效-展开&收缩    2023-11-06 12:15:46

jquery展开收缩的查看更多自定义折叠特效代码 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

jquery展开收缩的查看更多自定义折叠特效代码(共3个文件)

    • index.html

使用方法

(function ($) {
    let pluginName = 'myOwnLineShowMoreLess';

    let d = 0;

    let initShowMoreLess=function(data){
        let $that=$(this);
        let $thatDisplayVal=$that.css("display");
        let $thatFontSize=parseInt($that.css("font-size"),10);
        if($thatDisplayVal!=="inline"){
            $that.css({'display':'inline-block'});
        }
        let $span=$('<span>');
        let jsElem=$that.get(0);
        let compStyles = window.getComputedStyle(jsElem);
        let $thatLineHeight=compStyles.getPropertyValue('line-height');

        if($thatLineHeight=='normal'){
            var nodeName = jsElem.nodeName;
            var _node = document.createElement(nodeName);
            _node.innerHTML = '&nbsp;';

            let compStyles1 = window.getComputedStyle(jsElem);
            let fontSizeStr=compStyles1.getPropertyValue('font-size');
            _node.style.fontSize = fontSizeStr;

            _node.style.padding = '0px';
            _node.style.border = '0px';

            var body = document.body;
            body.appendChild(_node);

            var height = _node.offsetHeight;
            $thatLineHeight = height;

            body.removeChild(_node);
        }
        let $thatLineHeightInPx=parseInt($thatLineHeight);
        let restrictedLine=parseInt(data.settings.showLessLine);
        console.log($thatLineHeightInPx,restrictedLine);

        let currentState=(data.settings.lessAtInitial)?'less':'more';

        if(data.settings.lessAtInitial){
            $that.css({
                'max-height':($thatLineHeightInPx*restrictedLine)+'px',
                'overflow':'hidden',
                'position':'relative',
            });
            $span.html(data.settings.showMoreText);
            $span.attr('class','show-more-less-handler');
            $span.attr('style',' background: rgb(255,255,255);background: linear-gradient(90deg, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 17%, rgba(255,255,255,1) 100%); ');
            $span.css({
                'position':'absolute',
                'right':'0',
                'bottom':'0',
                'padding-left': '22px',
                'display':'inline-block',
                'cursor':'pointer',
            });
        }
        else{
            $that.css({
                'position':'relative'
            });
            $span.html(data.settings.showLessText);
            $span.attr('class','show-more-less-handler');
            $span.attr('style',' background: rgb(255,255,255);background: linear-gradient(90deg, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 17%, rgba(255,255,255,1) 100%); ');
            $span.css({
                'position':'static',
                'right':'0',
                'bottom':'0',
                'padding-left': '22px',
                'display':'inline-block',
                'cursor':'pointer'
            });
        }

        $span.on('click',function(){
            if (currentState=='less') {
                currentState='more';
                $that.css({
                    'position':'relative',
                    'overflow':'auto',
                    'max-height':'none'
                });
                $span.html(data.settings.showLessText);
                $span.attr('class','show-more-less-handler');
                $span.attr('style',' background: rgb(255,255,255);background: linear-gradient(90deg, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 17%, rgba(255,255,255,1) 100%); ');
                $span.css({
                    'position':'static',
                    'right':'0',
                    'bottom':'0',
                    'padding-left': '22px',
                    'display':'inline-block',
                    'cursor':'pointer'
                });
                if(data.settings.lessAtInitial && !data.settings.showLessAfterMore){
                    $span.remove();
                }
            }
            else{
                currentState='less';
                $that.css({
                    'max-height':($thatLineHeightInPx*restrictedLine)+'px',
                    'overflow':'hidden',
                    'position':'relative'
                });
                $span.html(data.settings.showMoreText);
                $span.css({
                    'position':'absolute',
                    'right':'0',
                    'bottom':'0',
                    'padding-left': '22px',
                    'display':'inline-block',
                    'cursor':'pointer'
                });
            }

        });
        $that.append($span);
    };

    let methods =
        {
            init : function(options)
            {
                //"this" is a jquery object on which this plugin has been invoked.
                return this.each(function(index)
                {
                    let $this = $(this);
                    let data = $this.data(pluginName);
                    // If the plugin hasn't been initialized yet
                    if (!data)
                    {
                        let settings =
                            {
                                showLessLine:1,
                                showLessText:'折叠查看',
                                showMoreText:'查看更多',
                                lessAtInitial:true,
                                showLessAfterMore:true,
                            };

                        if(options)
                        {
                            $.extend(true, settings, options);
                        }

                        $this.data(pluginName,
                            {
                                target : $this,
                                settings: settings
                            });

                        var $this2 = $(this)
                        var data2 = $this.data(pluginName);

                        initShowMoreLess.call($this2, data2);
                    }
                });
            }
        };

    $.fn[pluginName] = function( method )
    {
        if ( methods[method] )
        {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || !method )
        {
            return methods.init.apply( this, arguments );
        } else
        {
            $.error( 'Method ' + method + ' does not exist in jQuery.' + pluginName );
        }
    };
}(jQuery));

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 展开&收缩

jquery手风琴问答列表展开收缩特效

一款可显示隐藏的问答列表,带延迟动画特效,很实用!
  展开&收缩
 7266  0

jquery平滑展开收缩折叠动画的手风琴插件

一款手风琴动画特效插件,鼠标点击可展开隐藏的内容,再次点击可收缩。点击不同的标题可自动收缩。
  展开&收缩
 7300  0

jquery响应式适用于FAQ问答展开收缩特效代码

一款手风琴特效插件,点击标题可单独的触发展开收缩效果,还可一键展开/关闭,超实用。
  展开&收缩
 2553  0

jquery鼠标单击或悬停显示隐藏文本内容特效代码

一个功能强大的内容切换插件,通过单击箭头可展开显示隐藏的文本内容,或可以鼠标悬停显示。
  展开&收缩
 3315  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论