我在這裏遇到的問題是如何滾動到另一個關閉時單擊的div的頂部。如果沒有div打開,我可以使其工作,但如果其中一個是無法滾動到新打開的div的頂部。我假設我需要抵消舊的開放格。不知道如何去做。這裏是一個簡單的參考小提琴 - http://jsfiddle.net/dnym5p1s/3/滾動以點擊/打開div和由先前打開的div偏移
縮短代碼: HTML:
<div class="option-heading">
<h1>Year1 <span class="arrow-up">▲</span><span class="arrow-down">▼</span></h1>
</div>
<div class="option-content">ContentContentContent <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content</div>
<div class="option-heading">
<h1>Year2 <span class="arrow-up">▲</span><span class="arrow-down">▼</span></h1>
</div>
<div class="option-content">Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />vContent <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br />Content <br /></div>
CSS:
.option-heading h1 {
margin:0;
padding:8px 5px;
font-size:19px
}
.option-content h4 {
color: #900027;
font-weight: bold;
margin:25px 0 0
}
.option-content p {
margin-top:0
}
.option-content {
border-bottom: 10px solid #800202;
border-top: 10px solid #800202;
}
.arrow-up,.arrow-down{color:#647f8a;cursor:pointer;width:20px; font-size:10px; padding:0 0 0 10px;vertical-align:middle}
的jQuery:
$('.option-content,.arrow-up, .arrow-down:first').hide();
$('.option-content:first,.arrow-up:first').show();
$('.option-heading').click(function() {
$('.option-content').not($(this).next()).slideUp(250);
$('.option-heading').not(this).find('span.arrow-up').hide();
$('.option-heading').not(this).find('span.arrow-down').show();
$(this).next('.option-content').slideToggle(250);
$(this).find('.arrow-up, .arrow-down').toggle();
//Jump to Open Div
$('html,body').animate({scrollTop: $(this).offset().top - 10}, 'fast');
});
這正是我期待的。 – RooksStrife 2015-02-24 21:49:07