如何在Click上切换CSS3手风琴

时间:2013-12-07 15:56:09

标签: html css css3 click accordion

我有一个朋友给我的手风琴档案。这正是我要求的,一个不使用javascript的渐变标题栏的手风琴。我没有提到我需要手风琴垂直打开而不是水平打开,我想让它在点击时打开而不是悬停。他是一位知识渊博的朋友,但不知道如何实现这一目标。

我的CSS

.horizontalaccordion>ul {
    margin: 0;
    padding: 0;
    list-style:none;
    height: 300px;
}

.horizontalaccordion>ul>li {
    display:block;
    overflow: hidden;
    float:left;
    margin: 0;
    padding: 0;
    list-style:none;
    width:40px;
    height: 300px;

    /* Decorative CSS */
    background:#f0f0f0;

    /* CSS3 Transitions */
    transition: width 0.3s ease-in-out;
    -moz-transition: width 0.3s ease-in-out;
    -webkit-transition: width 0.3s ease-in-out;
    -o-transition: width 0.3s ease-in-out;
}

.horizontalaccordion>ul>li>h3 {
    display:block;
    float:left;
    margin: 0;
    padding:10px;
    height:19px;
    width:280px;

    /* Decorative CSS */
    border-left:#f0f0f0 1px solid;
    font-family: Arial, Helvetica, sans-serif;
    text-decoration:none;
    text-transform:uppercase;
    color: #fff;
    background:#cccccc;

    /* CSS3 Transform Rotate & Translate */
    white-space:nowrap;
    -moz-transform: rotate(90.0deg) translate(-40px,0px);  /* FF3.5+ */
    -moz-transform-origin: 0 100%;
    -o-transform: rotate(90.0deg) translate(-40px,0px);  /* Opera 10.5 */
    -o-transform-origin: 0 100%;
    -webkit-transform: rotate(90.0deg) translate(-40px,0px);  /* Saf3.1+, Chrome */
    -webkit-transform-origin: 0 100%;
    transform: rotate(90.0deg) translate(-40px,0px);  /* Saf3.1+, Chrome */
    transform-origin: 0 100%;
    filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1.0)
            progid:DXImageTransform.Microsoft.gradient(startColorstr=#ff002299, endColorstr=#ff0022cc);  /* IE6,IE7 */
    -ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=1.0)"
                "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ff002299, endColorstr=#ff0022cc)"; /* IE8 */

    /* CSS3 Gradient Effect */
    background: -moz-linear-gradient( top, #002299, #0022cc);
    background: -webkit-gradient(linear, left top, left bottom, from(#001199), to(#0011cc));
}

.horizontalaccordion>ul>li>div {
    display:none;
    float:left;
    overflow: auto;
    position:relative;
    top:-40px;
    left:40px;
    *top:0px;       /* IE7 Hack */
    *left:0px;      /* IE7 Hack */
    margin:0;
    width:320px;
    height:280px;
    padding:10px;
}

.horizontalaccordion>ul>li:hover {
    overflow: hidden;
    width: 380px;
}

.horizontalaccordion:hover>ul>li:hover>div {
    display:block;
}

.horizontalaccordion:hover>ul>li:hover>h3 {
    /* Decorative CSS */
    color:#fff;
    background:#000000;

    /* CSS3 Gradient Effect */
    background: -moz-linear-gradient( top, #000090, #000022); /* FF, Flock */
    background: -webkit-gradient(linear, left top, left bottom, from(#000090), to(#000022)); /* Safari, Chrome */
    filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1.0)
            progid:DXImageTransform.Microsoft.gradient(startColorstr=#ff000090, endColorstr=#ff000022); /* IE 5.5 - IE 7 */
    -ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=1.0)"
                "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ff000090, endColorstr=#ff000022)";   /* IE 8 */
}

.horizontalaccordion>ul>li>h3:hover {
    cursor:pointer;
}

我的HTML

<div class="horizontalaccordion">
<ul>
    <li>
        <h3>Header</h3>
        <div>
            Content
            <br /><center>
<a href="#">Link</a>
            </center></div>
    </li>
    <li>
        <h3>Header</h3>
        <div>
            Content
            <br /><center>
<a href="#">Link</a>
        </center></div>
    </li>
    <li>
        <h3>Header</h3>
        <div>
            Content
            <br /><center>
<a href="#">Link</a></center></div>
    </li>
    <li>
        <h3>Header</h3>
        <div>
            Content
            <br /><center>
        <a href="#">Link</a> </center></div>
    </li>
</ul>
</div>

如果有人可以帮我翻转这件事,我会很感激。

2 个答案:

答案 0 :(得分:6)

单击 - 切换动画手风琴(不含JS)

.ulAcc{
  width:300px;
  list-style:none;
  padding:0;
  margin:0;
}
.ulAcc li > label + input{
  display:none;
}
.ulAcc li > label{
  display:block;
  text-transform:uppercase;
  color:#fff;
  padding:10px;
  background:  #0022cc;
  background: -moz-linear-gradient( top, #002299, #0022cc);
  background: -webkit-gradient(linear, left top, left bottom, from(#001199), to(#0011cc));
}
.ulAcc li > div{
  overflow:hidden;
  height:0px;
  background:#ccc;
  transition: height 0.3s ease-in-out;
  -o-transition: height 0.3s ease-in-out;
  -moz-transition: height 0.3s ease-in-out;
  -webkit-transition: height 0.3s ease-in-out;
}
.ulAcc li > label + input:checked + div{
  height:300px;
}
<ul class="ulAcc">
      <li>
          <label for="a1">HEADER</label><input id="a1" type="radio" name="toggle">
          <div>Content</div>
      </li>
      <li>
          <label for="a2">HEADER</label><input id="a2" type="radio" name="toggle">
          <div>Content</div>
      </li>
      <li>
          <label for="a3">HEADER</label><input id="a3" type="radio" name="toggle">
          <div>Content</div>
      </li>
      <li>
          <label for="a4">HEADER</label><input id="a4" type="radio" name="toggle">
          <div>Content</div>
      </li>
</ul>

答案 1 :(得分:0)

您可以使用此代码:

DEMO

HTML中无需更改,这里是CSS:

h3, ul, html, body{
    margin:0;
    padding:0;
    list-style:none;
}
h3{
    background:blue;
    color:#fff;
    padding:10px;
    border:1px solid darkblue;
}
li div{
    padding:0;
    height:0;
    background:#eee;
    overflow:hidden;
    -webkit-transition:all 0.5s ease;
    -moz-transition:all 0.5s ease;
    -ms-transition:all 0.5s ease;
    -o-transition:all 0.5s ease;
    transition:all 0.5s ease;
}
li h3:hover + div, li div:hover{
    height:100px;
}