Đã lâu rồi, hôm nay mình mới quay trở lại với đề tài về bootstrap. Thật là một sự thiếu sót lớn khi mình giới thiệu về các thành phần hữu dụng của bootstrap 3 nhưng bỏ qua sự sụp đổ và đàn accordion. Vì vậy, bài viết này mình xin phép được bổ sung về cách tạo ra sự sụp đổ và đàn accordion với bootstrap 3
1. Sụp đổ
Để tạo thu gọn, chúng ta chỉ cần đoạn mã khá đơn giản như thế này
Accordion là gì? . Chúng ta thường gặp đàn accordion trong phần FAQ của trang web nào đó
Vui lòng xem đoạn mã dưới đây
<div class="faqs-page block col-md-6">
<div class="category-faq">Category 1</div>
<div class="panel-group" id="accordion2" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<a role="button" class="item-question" data-toggle="collapse" data-parent="#accordion2" href="#collapse1a" aria-expanded="true" aria-controls="collapse1a">
<span>1</span> The screen will either appear blank or flash between the story?
</a>
<div id="collapse1a" class="panel-collapse collapse in" role="tabpanel">
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</div>
</div>
</div>
<div class="panel panel-default">
<a class="item-question collapsed" role="button" data-toggle="collapse" data-parent="#accordion2" href="#collapse2a" aria-expanded="false" aria-controls="collapse2a">
<span>2</span> I purchased the event collection items from the item shop and I don't have it in my items?
</a>
<div id="collapse2a" class="panel-collapse collapse" role="tabpanel">
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et.
</div>
</div>
</div>
</div>
</div>
Trong đoạn mã trên, cần chú ý nhất đó là thuộc tính data-parent
Thuộc tính data-parent để biết khi bạn chọn nhóm này thì nhóm này được hiển thị và nhóm khác sẽ đóng lại
Ngoài ra, bạn hãy để thuộc tính aria-expanded. Thuộc tính này cho phép thu gọn bất kỳ trạng thái mặc định nào được mở khi đặt aria-expanded="true"
Tất nhiên để set status=open đối với 1 sập thì cái này chưa đủ, chúng ta cần thêm class. in into in div panel-collapse
Để trang điểm hoa lá cành thêm chút cho đoạn code demo, mình có thêm 1 đoạn css như sau
Trường hợp trên chúng ta chỉ mở được 1 lần thu gọn. Khi mở thu gọn khác, thu gọn hiện tại sẽ phải đóng lại
Nếu muốn mở nhiều lần thu gọn cùng lúc thì phải làm thế nào?
Như ở phần trên mình vừa giới thiệu. Thuộc tính data-parent để biết khi bạn chọn nhóm này thì nhóm này được hiển thị và nhóm khác sẽ đóng lại. Vì vậy, nếu muốn mở nhiều lần thu gọn cùng 1 lúc thì bạn chỉ cần bỏ thuộc tính data-parent này là được
<div class="faqs-page block col-md-6">
<div class="category-faq">Category 1</div>
<div class="panel-group" id="accordion2" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<a role="button" class="item-question collapsed" data-toggle="collapse" href="#collapse1a" aria-expanded="false" aria-controls="collapse1a">
<span>1</span> The screen will either appear blank or flash between the story?
</a>
<div id="collapse1a" class="panel-collapse collapse" role="tabpanel">
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</div>
</div>
</div>
<div class="panel panel-default">
<a class="item-question collapsed" role="button" data-toggle="collapse" href="#collapse2a" aria-expanded="false" aria-controls="collapse2a">
<span>2</span> I purchased the event collection items from the item shop and I don't have it in my items?
</a>
<div id="collapse2a" class="panel-collapse collapse" role="tabpanel">
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et.
</div>
</div>
</div>
</div>
</div>
Trong đoạn mã trên, mình đã bỏ thuộc tính data-parent này và để cho 2 lần thu gọn cùng ở trạng thái đóng. Và bây giờ cùng xem kết quả xem sao nhé
Như vậy, qua bài này, chúng ta đã biết thêm 1 thành phần khá hay của bootstrap. Nó rất dễ sử dụng. Chúc các bạn thành công