【问题标题】:How can I set this slide panel jQuery plugin to default close?如何将此滑动面板 jQuery 插件设置为默认关闭?
【发布时间】:2021-02-20 20:41:33
【问题描述】:

我已经实现了 DojoGeekRA 的面板切换脚本,该脚本发布在 JqueryScript.net(演示 https://www.jqueryscript.net/demo/Creating-A-Toggable-Bottom-Content-Panel-Using-jQuery-CSS/

就切换打开/关闭行为而言,它会根据需要发挥作用,但是当页面加载时它默认为打开状态,我需要它默认关闭

JS

(function($) {

jQuery(document).ready(function() {
    Panel.init();
    $(document).on('click', '.tab-controller', function() {
    Panel.togglePanel();
    });
});

var Panel = {
    isVisible : true,
    showMessage : null,
    hideMessage : null,
    animationDuration : 650,
    animationEasing : 'linear',
    init : function() {},

    hidePanel : function() {
        $('.panel-wrapper').animate({
            bottom : -(Panel.getAnimationOffset())
        }, Panel.animationDuration, Panel.animationEasing, function() {
            Panel.isVisible = false;
            Panel.updateTabMessage();
        });
    },

    showPanel : function() {
        $('.panel-wrapper').animate({
            bottom : 0
        }, Panel.animationDuration, Panel.animationEasing, function() {
            Panel.isVisible = true;
            Panel.updateTabMessage();
        });
    },

    togglePanel : function() {
        ((this.isVisible) ? this.hidePanel : this.showPanel)();
    },

    updateTabMessage : function() {
        if (this.isVisible) {
            $('.tab-controller .close').show();
            $('.tab-controller .show').hide();
        } else {
            $('.tab-controller .close').hide();
            $('.tab-controller .show').show();
        }
    },

    getAnimationOffset : function() {
        return $('.panel-content').height();
    }
}

})(jQuery);

我试过了

  • isVisible 设置为 false 但没有任何变化(是的,我刷新了页面)
  • .panel-content css 规则设置为display:none,虽然它响应默认隐藏,但 JS 仍处于打开模式,因此选项卡状态为 关闭 并在单击时关闭屏幕。李>

HTML

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>The Test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="plugin.css?v=9">
    <script src="jquery-1.11.0.min.js"></script>
    <script src="main.js?v=8"></script>
</head>
<body style="background: #ddd;">

<div class="panel-wrapper">
    <div class="panel-controller">
        <div class="tab-controller">
            <span class="close">Close</span>
            <span class="show">Open</span>
        </div>
    </div>
    
    <div class="panel-content">
        <div class="content clearfix">

        the content here

        </div>
    </div>
</div>

</body>
</html>

CSS

.panel-wrapper * {
    box-sizing: border-box;
}
.panel-wrapper {
    position: fixed;
    right: 0;
    bottom: 0;
    overflow: hidden;
    z-index: 99999;
    font-family: sans-serif;
}
.panel-controller {
    position: relative;
    overflow: hidden;
}
.tab-controller {
    float: right;
    margin-right: 50px;
    padding: 5px;
    background-color: #ff0000;
    border-radius: 5px 5px 0 0;
    display: block;
    font-size: 16px;
    font-weight: bold;
    color: white;
    cursor: pointer;
}
.tab-controller .show {
    display: none;
}
.panel-content {
    overflow: hidden;
}
.panel-content .content {
    overflow: hidden;
    margin: 0 5px 5px 0;
}

.clearfix:before, .clearfix:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}
.clearfix:after {
    clear: both;
}

更新

@The_Death_Raw 的回答完成了任务(谢谢),但是我需要能够动态设置选项并使用多个实例,所以我添加了一个函数闭包和设置变量。如果需要,这是修改后的工作脚本。

(function($) {

$.fn.bottomSlidePanel = function(options) 
{
    var wrap = this;
    
    return this.each(function() 
    {
        var setting = $.extend ({
            tab: ".tab-controller",
            contentarea: ".panel-content",
            defaultState: "close",
            animTime: 250
        }, options);
        
        $(function() {
            if( setting.defaultState === "close" ) {
                Panel.init(Panel.hidePanel(Panel.animationDuration = 0));
                
                setTimeout(function() {
                    Panel.hidePanel(Panel.animationDuration = setting.animTime); 
                }, 1);
            }else{
                Panel.init();
            }
            
            $(setting.tab).on("click", function() {
                Panel.togglePanel();
            });
        });

        var Panel = {
            isVisible : true,
            showMessage : null,
            hideMessage : null,
            animationDuration : setting.animTime,
            animationEasing : "linear",
            init : function() {},

            hidePanel : function() {
                $(wrap).animate({
                    bottom : -(Panel.getAnimationOffset())
                }, Panel.animationDuration, Panel.animationEasing, function() {
                    Panel.isVisible = false;
                    Panel.updateTabMessage();
                });
            },

            showPanel : function() {
                $(wrap).animate({
                    bottom : 0
                }, Panel.animationDuration, Panel.animationEasing, function() {
                    Panel.isVisible = true;
                    Panel.updateTabMessage();
                });
            },

            togglePanel : function() {
                ((this.isVisible) ? this.hidePanel : this.showPanel)();
            },

            updateTabMessage : function() {
                if (this.isVisible) {
                    $(setting.tab+' .tabclose').show();
                    $(setting.tab+' .tabshow').hide();
                } else {
                    $(setting.tab+' .tabclose').hide();
                    $(setting.tab+' .tabshow').show();
                }
            },

            getAnimationOffset : function() {
                return $(setting.contentarea).height();
            }
        }
    });
}

}(jQuery));

使用

基本使用核心默认值

jQuery(function($) {
  // attach to the parent wrap element
  $(".panel-wrapper").bottomSlidePanel(); 
});

使用选项

jQuery(function($) {
  // attach to the parent wrap element
  $(".panel-wrapper").bottomSlidePanel({
    tab: ".tab-controller", // set tab class or ID
    contentarea: ".panel-content", // set element class or ID
    defaultState: "open", // omit to allow default close
    animTime: 500 // (int) omit to use default value
  });
});

如果有人有能力使其更高效,请发布。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    隐藏面板,然后将 AnimationDuration 设置为 0 以在页面加载时隐藏。

    等待1s,然后将动画设置为650使其生效

    (function($) {
    
        jQuery(document).ready(function() {
            
            Panel.init(Panel.hidePanel(Panel.animationDuration = 0));
            
            setTimeout(function(){ Panel.hidePanel(Panel.animationDuration = 650); }, 1); 
            
            $(document).on('click', '.tab-controller', function() {
                 Panel.togglePanel();
            });
    
        });
    
        var Panel = {
    
            isVisible : true,
            showMessage : null,
            hideMessage : null,
            animationDuration : 650,
            animationEasing : 'linear',
    
            init : function() {
    
            },
    
            hidePanel : function() {
                $('.panel-wrapper').animate({
                    bottom : -(Panel.getAnimationOffset())
                }, Panel.animationDuration, Panel.animationEasing, function() {
                    Panel.isVisible = false;
                    Panel.updateTabMessage();
                });
            },
    
            showPanel : function() {
                $('.panel-wrapper').animate({
                    bottom : 0
                }, Panel.animationDuration, Panel.animationEasing, function() {
                    Panel.isVisible = true;
                    Panel.updateTabMessage();
                });
            },
    
            togglePanel : function() {
                ((this.isVisible) ? this.hidePanel : this.showPanel)();
            },
    
            updateTabMessage : function() {
                if (this.isVisible) {
                    $('.tab-controller .close').show();
                    $('.tab-controller .show').hide();
                } else {
                    $('.tab-controller .close').hide();
                    $('.tab-controller .show').show();
                }
            },
    
            getAnimationOffset : function() {
                return $('.panel-content').height();
            }
    
        }
    })(jQuery);
    .panel-wrapper * {
        box-sizing: border-box;
    }
    .panel-wrapper {
        position: fixed;
        right: 0;
        bottom: 0;
        overflow: hidden;
        z-index: 99999;
        font-family: sans-serif;
    }
    .panel-controller {
        position: relative;
        overflow: hidden;
    }
    .tab-controller {
        float: right;
        margin-right: 50px;
        padding: 5px;
        background-color: #ff0000;
        border-radius: 5px 5px 0 0;
        display: block;
        font-size: 16px;
        font-weight: bold;
        color: white;
        cursor: pointer;
    }
    .tab-controller .show {
        display: none;
    }
    .panel-content {
        overflow: hidden;
    }
    .panel-content .content {
        overflow: hidden;
        margin: 0 5px 5px 0;
    }
    
    .clearfix:before, .clearfix:after {
        content: " "; /* 1 */
        display: table; /* 2 */
    }
    .clearfix:after {
        clear: both;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <body style="background: #ddd;">
    
    <div class="panel-wrapper">
        <div class="panel-controller">
            <div class="tab-controller">
                <span class="close">Close</span>
                <span class="show">Open</span>
            </div>
        </div>
        
        <div class="panel-content">
            <div class="content clearfix">
              the content here
            </div>
        </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      • 2012-04-05
      • 2013-01-18
      • 1970-01-01
      • 2014-11-27
      相关资源
      最近更新 更多