我有一個我創建的示例手風琴;問題很簡單:slideUp()
動畫在第一次點擊任何手風琴項目時不起作用。我仍然不擅長鏈接jquery方法,所以我的問題是:我應該在哪裏鏈接slideUp()
方法?因爲添加和刪除類別名稱.active
父母使其複雜化。slideUp()動畫在第一次點擊時不起作用
$(document).ready(function() {
$("[class^='accordion-item'] > button").on('click', function() {
$(this).next().slideDown(400).parent().addClass('active').siblings().removeClass('active').children('div').slideUp(400);
})
})
* {
box-sizing: border-box;
outline: none;
}
#accordion {
width: 100%;
}
#accordion .accordion-item-1 button {
width: 100%;
border: none;
background: cornflowerblue;
color: #fff;
padding: 1rem 0;
position: relative;
-webkit-transition: box-shadow 400ms ease-in-out;
transition: box-shadow 400ms ease-in-out;
}
#accordion .accordion-item-1 button i {
position: absolute;
left: 1rem;
top: .6rem;
font-size: 2rem;
-webkit-transition: -webkit-transform 400ms ease-in-out;
transition: -webkit-transform 400ms ease-in-out;
transition: transform 400ms ease-in-out;
transition: transform 400ms ease-in-out, -webkit-transform 400ms ease-in-out;
}
#accordion .accordion-item-1 button:hover {
background: #3676e8;
cursor: pointer;
}
#accordion .accordion-item-1 div {
padding: 1rem;
background: #92b4f2;
color: #fff;
line-height: 2;
display: none;
}
#accordion .accordion-item-1.active button {
background: #3676e8;
box-shadow: 0 1px 5px 0 rgba(0, 0, 0, 0.4);
}
#accordion .accordion-item-1.active button i {
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
}
#accordion .accordion-item-1.active div {
display: block;
}
#accordion .accordion-item-2 button {
width: 100%;
border: none;
background: salmon;
color: #fff;
padding: 1rem 0;
position: relative;
-webkit-transition: box-shadow 400ms ease-in-out;
transition: box-shadow 400ms ease-in-out;
}
#accordion .accordion-item-2 button i {
position: absolute;
left: 1rem;
top: .6rem;
font-size: 2rem;
-webkit-transition: -webkit-transform 400ms ease-in-out;
transition: -webkit-transform 400ms ease-in-out;
transition: transform 400ms ease-in-out;
transition: transform 400ms ease-in-out, -webkit-transform 400ms ease-in-out;
}
#accordion .accordion-item-2 button:hover {
background: #f85441;
cursor: pointer;
}
#accordion .accordion-item-2 div {
padding: 1rem;
background: #fcaca3;
color: #fff;
line-height: 2;
display: none;
}
#accordion .accordion-item-2.active button {
background: #f85441;
box-shadow: 0 1px 5px 0 rgba(0, 0, 0, 0.4);
}
#accordion .accordion-item-2.active button i {
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
}
#accordion .accordion-item-2.active div {
display: block;
}
#accordion .accordion-item-3 button {
width: 100%;
border: none;
background: seagreen;
color: #fff;
padding: 1rem 0;
position: relative;
-webkit-transition: box-shadow 400ms ease-in-out;
transition: box-shadow 400ms ease-in-out;
}
#accordion .accordion-item-3 button i {
position: absolute;
left: 1rem;
top: .6rem;
font-size: 2rem;
-webkit-transition: -webkit-transform 400ms ease-in-out;
transition: -webkit-transform 400ms ease-in-out;
transition: transform 400ms ease-in-out;
transition: transform 400ms ease-in-out, -webkit-transform 400ms ease-in-out;
}
#accordion .accordion-item-3 button:hover {
background: #21653f;
cursor: pointer;
}
#accordion .accordion-item-3 div {
padding: 1rem;
background: #3bb16f;
color: #fff;
line-height: 2;
display: none;
}
#accordion .accordion-item-3.active button {
background: #21653f;
box-shadow: 0 1px 5px 0 rgba(0, 0, 0, 0.4);
}
#accordion .accordion-item-3.active button i {
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
}
#accordion .accordion-item-3.active div {
display: block;
}
#accordion .accordion-item-4 button {
width: 100%;
border: none;
background: violet;
color: #fff;
padding: 1rem 0;
position: relative;
-webkit-transition: box-shadow 400ms ease-in-out;
transition: box-shadow 400ms ease-in-out;
}
#accordion .accordion-item-4 button i {
position: absolute;
left: 1rem;
top: .6rem;
font-size: 2rem;
-webkit-transition: -webkit-transform 400ms ease-in-out;
transition: -webkit-transform 400ms ease-in-out;
transition: transform 400ms ease-in-out;
transition: transform 400ms ease-in-out, -webkit-transform 400ms ease-in-out;
}
#accordion .accordion-item-4 button:hover {
background: #e855e8;
cursor: pointer;
}
#accordion .accordion-item-4 div {
padding: 1rem;
background: #f4aff4;
color: #fff;
line-height: 2;
display: none;
}
#accordion .accordion-item-4.active button {
background: #e855e8;
box-shadow: 0 1px 5px 0 rgba(0, 0, 0, 0.4);
}
#accordion .accordion-item-4.active button i {
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
}
#accordion .accordion-item-4.active div {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<div id="accordion">
<div class="accordion-item-1 active">
<button><i class="fa fa-caret-right"></i><span>Item 1</span></button>
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident consectetur debitis a ipsam animi necessitatibus optio itaque amet? Aliquam quo nobis quis, quisquam necessitatibus commodi consequuntur dolor quae fugit numquam.</div>
</div>
<div class="accordion-item-2">
<button><i class="fa fa-caret-right"></i><span>Item 2</span></button>
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident consectetur debitis a ipsam animi necessitatibus optio itaque amet? Aliquam quo nobis quis, quisquam necessitatibus commodi consequuntur dolor quae fugit numquam.</div>
</div>
<div class="accordion-item-3">
<button><i class="fa fa-caret-right"></i><span>Item 3</span></button>
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident consectetur debitis a ipsam animi necessitatibus optio itaque amet? Aliquam quo nobis quis, quisquam necessitatibus commodi consequuntur dolor quae fugit numquam.</div>
</div>
<div class="accordion-item-4">
<button><i class="fa fa-caret-right"></i><span>Item 4</span></button>
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident consectetur debitis a ipsam animi necessitatibus optio itaque amet? Aliquam quo nobis quis, quisquam necessitatibus commodi consequuntur dolor quae fugit numquam.</div>
</div>
</div>
我沒有得到確切的問題 –
同樣在這裏,代碼片段按預期工作。第一個項目被擴展,當我點擊任何其他項目時,它立即擴展,而第一個項目收縮。 –
問題是因爲單擊紅色(或任何其他封閉部分)時*首先*開放部分立即關閉而不是向上滑動 –