0

我有一个页面,我希望能够使用持久标题,以及单击 adiv并让它像手风琴一样工作。当我单独使用它们时,我能够处理这两个部分,但是当我将它们放在一起时代码将不起作用

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");

    });

这是一个小提琴

4

2 回答 2

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);

在这里提琴

于 2012-12-22T06:53:23.233 回答
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

于 2012-12-26T19:00:55.753 回答