使用持久标题显示/隐藏内容

时间:2012-12-22 06:06:19

标签: jquery header accordion

我有一个页面,我希望能够使用持久性标题,同时单击div并使其像手风琴一样工作。当我单独使用它时,我能够处理这两个部分,但是当我把它们组合在一起时,代码将不起作用

HTML

<div class="main">
<div>
    <section>
        <h2 class="actog">Header</h2>
        <div class="accon">
            <!--Content Here-->
        </div>
    </section>
    <section>
        <h2 class="actog">Different Header</h2>
        <div class="accon">
            <!--Content Here-->
        </div>
    </section>
    <section>
        <h2 class="actog">Another Header</h2>
        <div class="accon">
            <!--Content Here-->
        </div>
    </section>
</div>
</div>

CSS

.actog {
    color:black;
    margin:5px 0;
    padding:5px;
    width:100%;
    height:auto;
    background-color:green;
    /* Transitions */
}
.actog:hover, .active{
    cursor:pointer;
    text-decoration:underline;
    color:#ff385b;
    background-color:pink;
}
.accon{padding:5px 0;}
.floatingHeader {
    position: fixed;
    margin-top: 0;
    top:0;
    visibility: hidden;
}

这两个jQuery片段

jQuery(document).ready(function() {
        jQuery(".actog").next(".accon").hide();
        jQuery(".actog").click(function(){
           $('.active').not(this).toggleClass('active').next('.accon').slideToggle(500);
            $(this).toggleClass('active').next().slideToggle(400);
        });
    });

function UpdateTableHeaders() {
       $(".main div section").each(function() {

           var el             = $(this),
               offset         = el.offset(),
               scrollTop      = $(window).scrollTop(),
               floatingHeader = $(".floatingHeader", this)

           if ((scrollTop > offset.top) && (scrollTop < offset.top + el.height())) {
               floatingHeader.css({
                "visibility": "visible"
               });
           } else {
               floatingHeader.css({
                "visibility": "hidden"
               });      
           };
       });
    }

    // DOM Ready      
    $(function() {

       var clonedHeaderRow;

       $(".main div section").each(function() {
           clonedHeaderRow = $(".actog", this);
           clonedHeaderRow
             .before(clonedHeaderRow.clone())
             .css("width", clonedHeaderRow.width())
             .addClass("floatingHeader");

       });

       $(window)
        .scroll(UpdateTableHeaders)
        .trigger("scroll");

    });

Here is A Fiddle

2 个答案:

答案 0 :(得分:1)

开,问题是您使用的是next()

$('.active').not(this).toggleClass('active').next('.accon').slideToggle(500);
$(this).toggleClass('active').next().slideToggle(400);

当您需要使用siblings()

$('.active').not(this).toggleClass('active').siblings('.accon').slideToggle(500);
$(this).toggleClass('active').siblings('.accon').slideToggle(400);

Fiddle here

答案 1 :(得分:0)

我提出了最终解决方案

<强> CSS

.actog {
color: black;
margin: 5px 0;
padding: 5px;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
background-color: green;
-webkit-transition: .25s;
-moz-transition: .25s;
-o-transition: .25s;
-ms-transition: .25s;
transition: .25s;
}
.actog:hover,.active,.activeClone {
cursor: pointer;
text-decoration: underline;
color: #ff385b;
background-color: pink;
}
.accon {padding: 5px 0; width: 100%;}
.floatingHeader {
position: fixed;
margin-top: 0;
top: 0;
visibility: hidden;
display: none;
}

<强> HTML

<div>
<section>
    <h2 id="1-clone" class="actog">Header</h2>
    <div class="accon">
        04/01/13<b></b>
        04/02/13<b></b>
        04/03/13<b></b>
        04/04/13<b></b>
        04/05/13<b></b>
        04/06/13<b></b>
        04/07/13<b></b>
        04/08/13<b></b>
        04/09/13<b></b>
        04/10/13<b></b>
        04/11/13<b></b>
        04/12/13<b></b>
        04/13/13<b></b>
        04/14/13<b></b>
        04/15/13<b></b>
        04/16/13<b></b>
        04/17/13<b></b>
        04/18/13<b></b>
        04/19/13<b></b>
        04/20/13<b></b>
        04/21/13<b></b>
        04/22/13<b></b>
    </div>
</section>
<section>
    <h2id="2-clone" class="actog">Different Header</h2>
    <div class="accon">
        04/01/13<b></b>
        04/02/13<b></b>
        04/03/13<b></b>
        04/04/13<b></b>
        04/05/13<b></b>
        04/06/13<b></b>
        04/07/13<b></b>
        04/08/13<b></b>
        04/09/13<b></b>
        04/10/13<b></b>
        04/11/13<b></b>
        04/12/13<b></b>
        04/13/13<b></b>
        04/14/13<b></b>
        04/15/13<b></b>
        04/16/13<b></b>
        04/17/13<b></b>
        04/18/13<b></b>
        04/19/13<b></b>
        04/20/13<b></b>
        04/21/13<b></b>
        04/22/13<b></b>
    </div>
</section>
<section>
    <h2 id="3-clone" class="actog">Another Header</h2>
    <div class="accon">
        04/01/13<b></b>
        04/02/13<b></b>
        04/03/13<b></b>
        04/04/13<b></b>
        04/05/13<b></b>
        04/06/13<b></b>
        04/07/13<b></b>
        04/08/13<b></b>
        04/09/13<b></b>
        04/10/13<b></b>
        04/11/13<b></b>
        04/12/13<b></b>
        04/13/13<b></b>
        04/14/13<b></b>
        04/15/13<b></b>
        04/16/13<b></b>
        04/17/13<b></b>
        04/18/13<b></b>
        04/19/13<b></b>
        04/20/13<b></b>
        04/21/13<b></b>
        04/22/13<b></b>
    </div>
</section>

<强> Jquery的

function UpdateTableHeaders() {
$(".main div section").each(function() {//select each section in a div in a container with class main
var el = $(this),
    offset = el.offset(),
     scrollTop = $(window).scrollTop();

    floatingHeader = $(".floatingHeader", this);

    var shadow = '#' + floatingHeader.attr('id').replace('-clone','-orig');
     if ((scrollTop > offset.top) && (scrollTop < offset.top + el.height()) && ($(shadow).hasClass('active'))) { //if the header has scrolled past visibility and it has the class of active, then target it
        floatingHeader.css({
            "visibility": "visible", "display":"block" //show header
        })
        .addClass('activeClone') //add class once has scrolled down
        .unbind('click') //remove the action of the first click
        .click(function(){
           $(shadow).click();
        });
    } else {
        floatingHeader.css({
            "visibility": "hidden", "display":"none" //hide header
        })
        .removeClass('activeClone'); //remove the class once scrolled back up
    };
});
}

$(function() {
var clonedHeaderRow;
$(".main div section").each(function() {
clonedHeaderRow = $(".actog", this);
var newId = clonedHeaderRow.attr('id').replace('-clone','-orig');
clonedHeaderRow
.before(clonedHeaderRow.clone().attr('id',newId))
.css("width", clonedHeaderRow.width())
.addClass("floatingHeader"); //add class
});
$(window).scroll(UpdateTableHeaders);
});

jQuery(document).ready(function() {
jQuery(".actog").siblings(".accon").hide();
jQuery(".actog").not('.floatingHeader').click(function() {
$('.active').not(this).toggleClass('active').siblings('.accon').slideToggle(500);
$(this).toggleClass('active').siblings('.accon').slideToggle(400);
});
});

http://codepen.io/burn123/pen/dsbpI