问题
I am looking for some help with collapsible I am trying to change the icon on the headers when they are active but cant seem to solve the problem of if I click on another header without closing the first one having the proper icon being displayed.
Here is a codepen with an example of my issue. http://codepen.io/FPC/pen/xZEWVY
here is my code:
<div class="container">
<div class="row">
<div class="col s6">
<ul class="collapsible popout" data-collapsible="accordion">
<li>
<div class="collapsible-header">
<i class="material-icons right more">expand_more</i>
<i class="material-icons right less" style="display: none">expand_less</i>
Article Title
</div>
<div class="collapsible-body">
<p>
Content Snipit
</p>
</div>
</li>
<li>
<div class="collapsible-header">
<i class="material-icons right more">expand_more</i>
<i class="material-icons right less" style="display: none">expand_less</i>
Article Title
</div>
<div class="collapsible-body">
<p>
Content Snipit
</p>
</div>
</li>
<li>
<div class="collapsible-header">
<i class="material-icons right more">expand_more</i>
<i class="material-icons right less" style="display: none">expand_less</i>
Article Title
</div>
<div class="collapsible-body">
<p>
Content Snipit
</p>
</div>
</li>
</ul>
</div>
</div>
</div>
here is the JS I am using
$(document).ready(function(){
$( ".collapsible-header" ).click(function() {
$(".more",this).toggle()
$(".less", this).toggle()
});
});
回答1:
You don't need the use javascript for this
delete javascript and use only css
html
<ul class="collapsible" data-collapsible="accordion">
<li>
<div class="collapsible-header">
<i class="material-icons">expand_less</i>First</div>
</li>
<li>
<div class="collapsible-header">
<i class="material-icons">expand_less</i>Second</div>
</li>
</ul>
css
.collapsible li.active i {
-ms-transform: rotate(180deg); /* IE 9 */
-webkit-transform: rotate(180deg); /* Chrome, Safari, Opera */
transform: rotate(180deg);
}
回答2:
As an addition to Hakan's answer, You can use the collapsible-header
class to avoid changing other icons within the collapsible class (accordion content). The CSS code that worked for me is:
CSS
.collapsible-header.active i {
-ms-transform: rotate(180deg); /* IE 9 */
-webkit-transform: rotate(180deg); /* Chrome, Safari, Opera */
transform: rotate(180deg);
}
回答3:
The above two solutions still flip other icons in the collapsible header the other way round. The perfect solution will be to add a class to the icon.
<div class="collapsible-header">
<i class="material-icons vertical-align">filter_drama</i>
Header 1
<i class="material-icons right expand">expand_less</i>
</div>
Notice that a "expand" class is created. CSS would be similar to the above two, just that "expand" will be created
.collapsible-header.active i.expand {
-ms-transform: rotate(180deg); /* IE 9 */
-webkit-transform: rotate(180deg); /* Chrome, Safari, Opera */
transform: rotate(180deg);
}
来源:https://stackoverflow.com/questions/34472456/materializecss-collapsible-active-change-icon