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

jquery点击按钮滑动打开显示侧边栏特效代码

一款响应式滑动式侧边栏插件,点击按钮可展开显示边栏模块,点击空白处或关闭可隐藏。
  展开&收缩
 141  

jquery可自定义的折叠展开插件

一款支持自定义的展开收缩插件,用户可以通过单击相应的标题元素来切换内容的显示隐藏,相互间可嵌套。
  展开&收缩
 130  

jquery鼠标滑过图片动画滑动展开收缩特效

鼠标hover经过自动拉伸展开对应内容,带弹性动画展开特效,效果很赞!
  展开&收缩
 163  

原生js实现的带平滑动画展开收缩特效代码

一款自定义的展开收缩文本截取插件,可自动识别高于200px的文本高度,指定位置显示展开收缩按钮。鼠标单击后触发平滑展开显示隐藏的文本文字内容。
  展开&收缩
 111  

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

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