只使用CSS切换手风琴图标

7
Ey,我有一个手风琴菜单,我想改变图标,我找到了一些人提出相同问题的答案,但我发现没有一个答案适用于我的情况。可能是因为我不知道在哪里应用代码。
我的菜单在Jsfiddle上:

http://jsfiddle.net/3wt0ehcj/

我尝试获取的字形图标:

https://codepen.io/tofanelli/pen/waadRY

我需要放置的代码:

.panel-heading .accordion-toggle:after {
    /* symbol for "opening" panels */
    font-family: 'Glyphicons Halflings';  /* essential for enabling glyphicon */
    content: "\e114";    /* adjust as needed, taken from bootstrap.css */
    float: right;        /* adjust as needed */
    color: grey;         /* adjust as needed */
}
.panel-heading .accordion-toggle.collapsed:after {
    /* symbol for "collapsed" panels */
    content: "\e080";    /* adjust as needed, taken from bootstrap.css */
}

如果有其他更简单的方法,我全耳朵听! :)

1
当您单击时,可以使用Javascript / Jquery切换类。 - Thanveer Shah
你能够实现它了吗? - Thanveer Shah
@thanveershah,我已经查找了如何使用jquery进行操作,但是没有找到我能理解的内容。但是我只需要将CSS代码放在自己的某个位置,只是找不到在哪里。 - J. Coderino
如果您想要,我可以使用JQuery创建一个。 - Thanveer Shah
图标是使用CSS创建的。 - Temani Afif
3个回答

2
您可以使用 :not(checked)checked CSS 伪类来解决此问题。
  1. :not()
  2. :checked
.accordion-bral input:checked ~ .ac-label i:before {
  transform: translate(2px, 0) rotate(-45deg);

}
.accordion-bral input:checked ~ .ac-label i:after {
  transform: translate(-2px, 0) rotate(45deg);
}
.accordion-bral i:before, .accordion-bral i:after {
  content: "";
  position: absolute;
  background-color: #808080;
  width: 3px;
  height: 9px;
}
.accordion-bral i:before {
  transform: translate(-2px, 0) rotate(-45deg);
}
.accordion-bral i:after {
  transform: translate(2px, 0) rotate(45deg);
}

使用glyphicon源代码,然后使用此代码。

   /* when input is checked */
.accordion-bral input:checked ~ .ac-label i:after {
   content: "\e114" 
}

/* when input is not checked */
.accordion-bral input:not(checked) ~ .ac-label i:after {
   content: "\e080"; 
}
.accordion-bral i:after {
  font-style: normal;  /* change font style too */
  font-family: 'Glyphicons Halflings';  /* essential for enabling glyphicon */
  content: "\e114";    /* adjust as needed, taken from bootstrap.css */
  float: right;        /* adjust as needed */
  color: grey;  
}

http://jsfiddle.net/7zsbumax/2/


.accordion-bral {
  min-height: 0;
  min-width: 220px;
  width: 100%;
  height: 100%;
  background-color: #FFF;
  margin: 0px!important;
}

.accordion-bral .ac-label {
  font-family: Arial, sans-serif;
  padding: 5px 20px;
  position: relative;
  display: block;
  height: auto;
  cursor: pointer;
  color: #777;
  line-height: 33px;
  font-size: 19px;
  background: #EFEFEF;
  border: 1px solid #CCC;
}

.accordion-bral .ac-label:hover {
  background: #b70000;
  color: white;
}

.accordion-bral input+.ac-label {
  -webkit-transition: all 0.3s ease-in-out;
  -moz-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  -ms-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

.accordion-bral input:checked+.ac-label,
.accordion-bral input:checked+.ac-label:active {
  background-color: #b70000;
  color: #FFF;
  box-shadow: 0px 0px 0px 1px rgba(155, 155, 155, 0.3), 0px 2px 2px rgba(0, 0, 0, 0.1);
}

.accordion-bral input.ac-input {
  display: none;
}

.accordion-bral .article {
  background: white;
  overflow: hidden;
  height: 20px;
  max-height: auto;
  -webkit-transition: all 0.3s ease-in-out;
  -moz-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  -ms-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

.accordion-bral .article p {
  color: #777;
  line-height: 23px;
  font-size: 14px;
  padding: 20px;
}

.accordion-bral input:checked~.article i {
  -webkit-transition: all 0.5s ease-in-out;
  -moz-transition: all 0.5s ease-in-out;
  -o-transition: all 0.5s ease-in-out;
  -ms-transition: all 0.5s ease-in-out;
  transition: all 0.5s ease-in-out;
}

.accordion-bral input:checked~.article.ac-content {
  height: auto;
}

.accordion-bral i {
  text-decoration: none;
  margin-top: 16px;
  right: 0;
}

.accordion-bral input:checked~.ac-label i:after {
  content: "\e114"
}

.accordion-bral input:not(checked)~.ac-label i:after {
  content: "\e080";
}

.accordion-bral i:after {
  font-style: normal;
  font-family: 'Glyphicons Halflings';
  /* essential for enabling glyphicon */
  content: "\e114";
  /* adjust as needed, taken from bootstrap.css */
  float: right;
  /* adjust as needed */
  color: grey;
}

ul.ac-list {
  padding-left: 40px;
  list-style-type: disc;
}

table.ac-table {
  margin: 20px 0 20px 20px;
}

table.ac-table th {
  text-align: left;
}

@media (max-width: 550px) {
  .accordion-bral .ac-label {
    font-family: Arial, sans-serif;
    padding: 5px 20px;
    position: relative;
    display: block;
    height: auto;
    padding-right: 40px;
    cursor: pointer;
    color: #777;
    line-height: 33px;
    font-size: 19px;
    background: #EFEFEF;
    border: 1px solid #CCC;
  }
  .accordion-bral i {
    position: absolute;
    transform: translate(-30px, 0);
    margin-top: 2%;
    right: 0;
  }
}
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" rel="stylesheet">


<div class="accordion-bral">

  <div>
    <!-- accordion item 1 -- start -->
    <input class="ac-input" id="ac-1" name="accordion-1" type="checkbox" checked/>
    <label class="ac-label" for="ac-1">HTML and CSS only<i></i></label>
    <div class="article ac-content">
      <h1 style="position: absolute; margin-left:10px;color:#b70000;">HOUTSOORT</h1>

      <div class="flex-container">
        <div class="card">
          <img src="maxopdracht2/eiken-vloer.jpg" style="width:100%">
          <div>
            <input id="checkbox-1" class="checkbox-custom" style="" name="checkbox-1" type="checkbox">
            <label for="checkbox-1" class="checkbox-custom-label">Eiken</label>
          </div>
        </div>
        <div class="card">
          <img src="maxopdracht2/beuken-vloer.jpg" style="width:100%">
          <div>
            <input id="checkbox-1" class="checkbox-custom" style="" name="checkbox-1" type="checkbox">
            <label for="checkbox-1" class="checkbox-custom-label">Beuken</label>
          </div>
        </div>
        <div class="card">
          <img src="maxopdracht2/grenen-vloer.jpg" style="width:100%">
          <div>
            <input id="checkbox-1" class="checkbox-custom" style="" name="checkbox-1" type="checkbox">
            <label for="checkbox-1" class="checkbox-custom-label">Grenen</label>
          </div>
        </div>
        <div class="card">
          <img src="maxopdracht2/maple-vloer.jpg" style="width:100%">
          <div>
            <input id="checkbox-1" class="checkbox-custom" style="" name="checkbox-1" type="checkbox">
            <label for="checkbox-1" class="checkbox-custom-label">Maple</label>
          </div>
        </div>
        <div class="card">
          <img src="maxopdracht2/merbau-vloer.jpg" style="width:100%">
          <div>
            <input id="checkbox-1" class="checkbox-custom" style="" name="checkbox-1" type="checkbox">
            <label for="checkbox-1" class="checkbox-custom-label">Merbau</label>
          </div>
        </div>
        <div class="card">
          <img src="maxopdracht2/onbekend-vloer.jpg" style="width:100%">
          <div>
            <input id="checkbox-1" class="checkbox-custom" style="" name="checkbox-1" type="checkbox">
            <label for="checkbox-1" class="checkbox-custom-label">Overig/onbekend</label>
          </div>
        </div>

      </div>
    </div>
  </div>
  <!-- accordion item 1 -- end -->

  <div>
    <!-- accordion item 2 -- start -->
    <input class="ac-input" id="ac-2" name="accordion-1" type="checkbox" />
    <label class="ac-label" for="ac-2">responsive accordion<i></i></label>
    <div class="article ac-content">

    </div>
  </div>
  <!-- accordion item 2 -- end -->

  <div>
    <!-- accordion item 3 -- start -->
    <input class="ac-input" id="ac-3" name="accordion-1" type="checkbox" />
    <label class="ac-label" for="ac-3">Divs to divide your things up<i></i></label>
    <div class="article ac-content">

    </div>
  </div>
  <!-- accordion item 3 -- end -->

  <div>
    <!-- accordion item 4 -- start -->
    <input class="ac-input" id="ac-4" name="accordion-1" type="checkbox" />
    <label class="ac-label" for="ac-4">Forms are cool<i></i></label>
    <div class="article ac-content">

    </div>
  </div>
</div>

更新

如果您想要一些动画效果,请使用以下代码:

.accordion-bral input:checked ~ .ac-label i:after {
  /*  transform: rotate(90deg); */
  transform: rotate(-270deg);
}

.accordion-bral i:after {
  content: "\e080"; 
  font-style: normal;
  font-family: 'Glyphicons Halflings';  /* essential for enabling glyphicon */
  float: right;        /* adjust as needed */
  color: grey;  
  transition: 1s ease-out;
}

http://jsfiddle.net/2eLb3ju7/


@J.Coderino 欢迎。已更新答案。 - mahan

-1

第一步:

您是否在HTML文件中包含了Gryphicons的链接? 您需要将以下链接写入<head>标记中。 通常是在<meta>标记之后。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

第二步:

当然,标记应该包含类似于:

<p class="panel-heading"></p>

第三步:您的CSS代码应该在<style>标签内(如果不在.css文件中)。
点击此处阅读更多信息:https://www.w3schools.com/bootstrap/bootstrap_ref_comp_glyphs.asp

-1
你需要调整代码的这一部分:
.accordion-bral input:checked ~ .ac-label i:before {
  transform: translate(2px, 0) rotate(-45deg);

}
.accordion-bral input:checked ~ .ac-label i:after {
  transform: translate(-2px, 0) rotate(45deg);
}
.accordion-bral i:before, .accordion-bral i:after {
  content: "";
  position: absolute;
  background-color: #808080;
  width: 3px;
  height: 9px;
}
.accordion-bral i:before {
  transform: translate(-2px, 0) rotate(-45deg);
}
.accordion-bral i:after {
  transform: translate(2px, 0) rotate(45deg);
}

转换为类似这样的形式:

.accordion-bral input:checked ~ .ac-label i:before {
  content: "+"; /*Icon for the opened state*/

}

.accordion-bral i:before {
  content: "-"; /*Icon for the closed state*/
}

同样移除与 :after 元素相关的CSS。

包含 Font Awesome 示例的完整代码:

.accordion-bral {
  min-height: 0;
  min-width: 220px;
  width: 100%;
  height: 100%;
  background-color: #FFF;
  margin: 0px!important;
}
.accordion-bral .ac-label {
  font-family: Arial, sans-serif;
  padding: 5px 20px;
  position: relative;
  display: block;
  height: auto;
  cursor: pointer;
  color: #777;
  line-height: 33px;
  font-size: 19px;
  background: #EFEFEF;
  border: 1px solid #CCC;
}
.accordion-bral .ac-label:hover {
  background: #b70000;
  color: white;
}
.accordion-bral input + .ac-label  {
  -webkit-transition: all 0.3s ease-in-out;
  -moz-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  -ms-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}
.accordion-bral input:checked + .ac-label,
.accordion-bral input:checked + .ac-label:active {
  background-color: #b70000;
  color: #FFF;
  box-shadow: 0px 0px 0px 1px rgba(155, 155, 155, 0.3), 0px 2px 2px rgba(0, 0, 0, 0.1);
}
.accordion-bral input.ac-input {
  display: none;
}
.accordion-bral .article {
  background: white;
  overflow: hidden;
  height: 20px;
  max-height: auto;
  -webkit-transition: all 0.3s ease-in-out;
  -moz-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  -ms-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}
.accordion-bral .article p {
  color: #777;
  line-height: 23px;
  font-size: 14px;
  padding: 20px;
}
.accordion-bral input:checked ~ .article i {
  -webkit-transition: all 0.5s ease-in-out;
  -moz-transition: all 0.5s ease-in-out;
  -o-transition: all 0.5s ease-in-out;
  -ms-transition: all 0.5s ease-in-out;
  transition: all 0.5s ease-in-out;
}
.accordion-bral input:checked ~ .article.ac-content {
  height: auto;
}

.accordion-bral i {
  position: absolute;
  transform: translate(-30px, 0);
  margin-top: 16px;
  right: 0;
    font-style: initial;
}
.accordion-bral input:checked ~ .ac-label i:before {
  content: "\f078";
  
}

.accordion-bral i:before {
  content: "\f054";
  position: absolute;
  top:-15px;
  font-family:"Font Awesome 5 Free";
  font-weight:900;
}

ul.ac-list {
  padding-left: 40px;
  list-style-type: disc;
}

table.ac-table {
  margin: 20px 0 20px 20px;
}
table.ac-table th{
  text-align: left;
}

@media (max-width: 550px) {
  .accordion-bral .ac-label {
  font-family: Arial, sans-serif;
  padding: 5px 20px;
  position: relative;
  display: block;
  height: auto;
  padding-right: 40px;
  cursor: pointer;
  color: #777;
  line-height: 33px;
  font-size: 19px;
  background: #EFEFEF;
  border: 1px solid #CCC;
}
  .accordion-bral i {
  position: absolute;
  transform: translate(-30px, 0);
  margin-top: 2%;
  right: 0;
}
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" >
<div class="accordion-bral">

 <div>
   <!-- accordion item 1 -- start -->
   <input class="ac-input" id="ac-1" name="accordion-1" type="checkbox" checked/>
   <label class="ac-label" for="ac-1">HTML and CSS only<i></i></label>
   <div class="article ac-content">
     <h1 style="position: absolute; margin-left:10px;color:#b70000;">HOUTSOORT</h1>

<div class="flex-container">
     <div class="card">
       <img src="maxopdracht2/eiken-vloer.jpg" style="width:100%">
       <div>
         <input id="checkbox-1" class="checkbox-custom" style="" name="checkbox-1" type="checkbox">
         <label for="checkbox-1" class="checkbox-custom-label">Eiken</label>
       </div>
     </div>
     <div class="card">
     <img src="maxopdracht2/beuken-vloer.jpg" style="width:100%">
     <div>
       <input id="checkbox-1" class="checkbox-custom" style="" name="checkbox-1" type="checkbox">
       <label for="checkbox-1" class="checkbox-custom-label">Beuken</label>
     </div>
     </div>
     <div class="card">
     <img src="maxopdracht2/grenen-vloer.jpg" style="width:100%">
     <div>
       <input id="checkbox-1" class="checkbox-custom" style="" name="checkbox-1" type="checkbox">
       <label for="checkbox-1" class="checkbox-custom-label">Grenen</label>
     </div>
     </div>
     <div class="card">
     <img src="maxopdracht2/maple-vloer.jpg" style="width:100%">
     <div>
       <input id="checkbox-1" class="checkbox-custom" style="" name="checkbox-1" type="checkbox">
       <label for="checkbox-1" class="checkbox-custom-label">Maple</label>
     </div>
     </div>
     <div class="card">
     <img src="maxopdracht2/merbau-vloer.jpg" style="width:100%">
     <div>
       <input id="checkbox-1" class="checkbox-custom" style="" name="checkbox-1" type="checkbox">
       <label for="checkbox-1" class="checkbox-custom-label">Merbau</label>
     </div>
     </div>
     <div class="card">
       <img src="maxopdracht2/onbekend-vloer.jpg" style="width:100%">
       <div>
         <input id="checkbox-1" class="checkbox-custom" style="" name="checkbox-1" type="checkbox">
         <label for="checkbox-1" class="checkbox-custom-label">Overig/onbekend</label>
       </div>
     </div>

   </div>
 </div>
 </div>
 <!-- accordion item 1 -- end -->

 <div>
   <!-- accordion item 2 -- start -->
   <input class="ac-input" id="ac-2" name="accordion-1" type="checkbox" />
   <label class="ac-label" for="ac-2">responsive accordion<i></i></label>
   <div class="article ac-content">

   </div>
 </div>
 <!-- accordion item 2 -- end -->

 <div>
   <!-- accordion item 3 -- start -->
   <input class="ac-input" id="ac-3" name="accordion-1" type="checkbox" />
   <label class="ac-label" for="ac-3">Divs to divide your things up<i></i></label>
   <div class="article ac-content">

   </div>
 </div>
 <!-- accordion item 3 -- end -->

 <div>
   <!-- accordion item 4 -- start -->
   <input class="ac-input" id="ac-4" name="accordion-1" type="checkbox" />
   <label class="ac-label" for="ac-4">Forms are cool<i></i></label>
   <div class="article ac-content">

     </div>
   </div>
 </div>


这些是加号和减号图标。但我在信息中放置的CodePen已经用>替换了向上箭头,而那是我想要的。 - J. Coderino
@J.Coderino 我知道,我使用+/-来使代码简单化,现在只需用你想要的任何图标进行替换。我会展示如何调整(请查看片段)。 - Temani Afif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接