当文档准备就绪时,如何使Jquery可折叠面板折叠而不是展开?

时间:2012-04-24 21:54:23

标签: javascript jquery expand collapsable

我有以下jquery代码插件:

           (function ($) {
                $.fn.extend({
                    collapsiblePanel: function () {
                        // Call the ConfigureCollapsiblePanel function for the selected element
                        return $(this).each(ConfigureCollapsiblePanel);
                    }
                });
            })(jQuery);

            function ConfigureCollapsiblePanel() {
                $(this).addClass("ui-widget");

                // Check if there are any child elements, if not then wrap the inner text within a new div.
                if ($(this).children().length == 0) {
                    $(this).wrapInner("<div></div>");
                }

                // Wrap the contents of the container within a new div.
                $(this).children().wrapAll("<div class='collapsibleContainerContent ui-widget-content'></div>");

                // Create a new div as the first item within the container.  Put the title of the panel in here.
                $("<div class='collapsibleContainerTitle ui-widget-header'><div>" + $(this).attr("title") + "</div></div>").prependTo($(this));


                // Assign a call to CollapsibleContainerTitleOnClick for the click event of the new title div.
                $(".collapsibleContainerTitle", this).click(CollapsibleContainerTitleOnClick);
            }


            function CollapsibleContainerTitleOnClick() {
                // The item clicked is the title div... get this parent (the overall container) 
                // and toggle the content within it.
                $(".collapsibleContainerContent", $(this).parent()).slideToggle();
            }

然后是html:

<div class="collapsibleContainer" title="Example Collapsible Panel">
                                    <p>
                                        KDLorem ipsum dolor sit amet, consectetur adipiscing elit.... (you get the idea!)
                                    </p>
                                </div>  

然后是css:

.collapsibleContainer
{
}

.collapsibleContainerTitle
{
   cursor:pointer;
}

.collapsibleContainerTitle div
{
  padding-top:5px;
  padding-left:10px;
}

.collapsibleContainerContent
{
   padding: 10px;
}

这是文档就绪功能:

jQuery(function () {
   jQuery(".collapsibleContainer").collapsiblePanel();
});

我需要编写什么才能让Panel在文档准备就绪时实际崩溃而不是像文档加载时那样进行扩展?

1 个答案:

答案 0 :(得分:3)

将面板内容设置为display:none

该插件看起来使用了slideToggle(),它只是动画转换为display:none的转换。

所以,我想这个:

.collapsibleContainerContent
{
   padding: 10px;
   display: none;
}