【问题标题】:jQuery collapsible panel (my own) problemsjQuery 可折叠面板(我自己的)问题
【发布时间】:2011-11-09 04:40:44
【问题描述】:

我正在尝试创建一个 jQuery 可折叠面板,虽然周围有插件,但我还没有找到一个完全符合我要求的插件,我正在尝试使用我找到的各种示例自行开发。虽然它在大多数情况下都有效,但有一点无效,我不知道为什么。

(示例页面位于http://www.thekmz.co.uk/misc/cpanel/index1.html

它是如何工作的(或者可能不是这样)

HTML

<div id="container">
    <div class="collapsiblePanel" title="Example Collapsible Panel">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit... etc</p>
    </div>
</div>

Javascript

(function($) {
    $.fn.extend({
        collapsiblePanel: function() {
        return $(this).each(initCollapsiblePanel);
    }
  });
})(jQuery);

function initCollapsiblePanel() {
    if ($(this).children().length == 0) {
        $(this).wrapInner("<div></div>");
    }    
    $(this).children().wrapAll("<div class='cpanelContent'></div>");
    $("<div class='cpanelTitle'><div>" + $(this).attr("title") + "</div><div class='cpanelToggle'>more</div></div>").prependTo($(this));
    $(".cpanelContent", this).hide();
    $(".cpanelTitle", this).click(toggleCollapsiblePanel);
}

function toggleCollapsiblePanel() {
    $(".cpanelContent", $(this).parent()).slideToggle();

    if ($(".cpanelContent", $(this).parent()).is(":hidden")) {
        $(".cpanelToggle", this).html('more');
    } else {
        $(".cpanelToggle", this).html('close');
    }
}

CSS

#container {
    width:300px;
    height:300px;
}

.collapsiblePanel {
    width:100%;
}

.cpanelTitle {
    position:relative;
    width:100%;
    cursor: pointer;
    cursor: hand;
}

.cpanelToggle {
    position:absolute;
    top:0px;
    right:0px;
    font-style:italic;
}

所以它需要一个带有 collapsiblePanel 类的 div 并创建各种嵌套的 div。标题 div 包含标题和一些向右浮动的切换文本,根据内容面板是展开还是折叠,这些文本将是“更多”或“关闭”。

不起作用的位是

if ($(".cpanelContent", $(this).parent()).is(":hidden")) {

在 toggleCollapsiblePanel() 函数中。 div 切换正常,所以看起来我选择了内容 div,但使用它来确定它是否被隐藏(所以我可以更改 cPanelToggleDiv 的文本)总是返回为 false - 这向我表明我只是做得不对。

我在那里(在上面链接的示例页面中)放了很多控制台调用,试图看看发生了什么,但就是看不到它。

任何指出我的绝望/困惑的帮助将不胜感激。

问候 尼莫

【问题讨论】:

  • 您可以使用现有的,最适合您的需求,并对其进行修改。也许这会更容易。
  • 使用 Chrome,它似乎对我有用。在初始页面加载时控制台显示isHidden: true,然后打开它后显示isHidden: false
  • @Wulf - 在某种程度上我正在尝试 - 我已经组合了我发现的 2 个不同的,但结果 div 布局不同,所以我必须使用不同的选择器
  • @Joseph - 看起来是这样,但 isHidden: true 来自初始化函数。所有 isHidden: false 的东西都在 toggleCollapsiblePanel() 函数中,它说 false 不管

标签: jquery jquery-selectors collapse


【解决方案1】:

您应该在slideToggle 回调方法中检查hidden 状态。除非幻灯片动画完成,否则您不能说它是否隐藏。试试这个

在这里我也做了一些其他的优化,比如使用this,它将指向它正在滑动的元素。

function toggleCollapsiblePanel() {
    $(".cpanelContent", $(this).parent()).slideToggle(function(){
    if ($(this).is(":hidden")) {
           $(this).html('more');
       } else {
           $(this).html('close');
       }
   });
}

【讨论】:

  • 啊……谢谢尚卡尔。我现在已经对其进行了更改,因此它在回调中并且可以正常工作。我仍然必须使用 $(".cpanelToggle", $(this).parent()).html('more');而不是 $(this).html('more');虽然该文本在标题中的 div 中,而不是在滑动的元素中 - 但做得很好 - thx
  • ps ...抱歉不能投票,因为我是一个完全的菜鸟,我还没有声望
  • Np @Nymor,很高兴它帮助了你。
【解决方案2】:

添加

    var open = false; 

到您的 initCollapsiblePanel 函数。

然后更新toggleCollapsiblePanel函数replace:

    if ($(".cpanelContent", $(this).parent()).is(":hidden")) {... 

以下内容:

if (!open) {
    $(".cpanelToggle", this).html('more');
    open  = true;
} else {
    $(".cpanelToggle", this).html('close');
    open = false;

}

这现在应该改变更多和关闭的文本。(我认为这是你的追求)。 硅

【讨论】:

    【解决方案3】:

    问题是您的测试在 slideToggle 完成运行之前正在运行。这意味着它要么刚刚变得可见,要么正在淡出,在任何一种情况下都不会被隐藏。

    您应该将文本切换到 slideToggle 回调函数中,例如:

    function toggleCollapsiblePanel() {
        $(".cpanelContent", $(this).parent()).slideToggle(600, function() {
            if ($(this).is(":hidden")) {
                $(this).html('more');
            } else {
                $(this).html('close');
            }   
        });    
    }
    

    【讨论】:

    • thx Timbo - 就像 Shankar 所说的那样使用回调 - 再次感谢。
    【解决方案4】:

    WORKING DEMO

    代码:

    function toggleCollapsiblePanel() {
        var cCp = $(".cpanelContent", $(this).parent());
        var cT  = $(".cpanelToggle" , this);
    
        check = cCp.is(":hidden") ? cT.html('close') : cT.html('more') ;
        cCp.slideToggle();
    }
    

    【讨论】:

    • thx roXon - 另一种选择 - 只需要考虑我是否希望在幻灯片(回调)之后或之前(您的方法)更改更多/关闭标签。很高兴有两种选择 - 干杯。
    • P.S.我改进了我的代码。现在更具可读性。你也可以这样做:cCp.is(":visible") ? cT.html('more') : cT.html('close') ;
    猜你喜欢
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多