1

我创建了一个带有手风琴式过渡的导航菜单,它是用 CSS3 制作的。我对此很满意,效果很好。这是我制作的代码的实时版本:http: //codepen.io/anon/pen/EJoxr

我想通过添加一个新功能来改进它:我想弹出我点击的 div 的文章,也想弹出以前的 div。

我的意思是,如果我点击名为“联系我们”的第 4 个 div,我想显示所有文章。单击第 2 个,仅显示第 1 个和第 2 个。

可以用 CSS3 做到这一点吗?

HTML

<div class="container">
    <section class="ac-container">
        <div>
            <input id="ac-1" name="accordion-1" type="checkbox" />
            <label for="ac-1">About us</label>
            <article class="ac-small">
                <p>Well, the way they make shows is, they make one show. That show's called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they decide if they're going to make more shows.</p>
            </article>
        </div>
        <div>
            <input id="ac-2" name="accordion-1" type="checkbox" />
            <label for="ac-2">How we work</label>
            <article class="ac-medium">
                <p>Like you, I used to think the world was this great place where everybody lived by the same standards I did, then some kid with a nail showed me I was living in his world, a world where chaos rules not order, a world where righteousness is not rewarded. That's Cesar's world, and if you're not willing to play by his rules, then you're gonna have to pay the price. </p>
            </article>
        </div>
        <div>
            <input id="ac-3" name="accordion-1" type="checkbox" />
            <label for="ac-3">References</label>
            <article class="ac-large">
                <p>You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder. After the avalanche, it took us a week to climb out. Now, I don't know exactly when we turned on each other, but I know that seven of us survived the slide... and only five made it out. Now we took an oath, that I'm breaking now. We said we'd say it was the snow that killed the other two, but it wasn't. Nature is lethal but it doesn't hold a candle to man. </p>
            </article>
        </div>
        <div>
            <input id="ac-4" name="accordion-1" type="checkbox" />
            <label for="ac-4">Contact us</label>
            <article class="ac-large">
                <p>You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. </p>
            </article>
        </div>
    </section>
</div>

CSS

div {
  display: block;
  width: 25%;
  float:left;
}
.ac-container{
    width: 900px;
    margin: 10px auto 30px auto;
    text-align: left;
}
.ac-container label{
    font-family: 'BebasNeueRegular', 'Arial Narrow', Arial, sans-serif;
    padding: 5px 20px;
    position: relative;
    z-index: 20;
    display: block;
    height: 30px;
    cursor: pointer;
    color: #777;
    text-shadow: 1px 1px 1px rgba(255,255,255,0.8);
    line-height: 33px;
    font-size: 19px;
    background: #ffffff;
    background: -moz-linear-gradient(top, #ffffff 1%, #eaeaea 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,#ffffff), color-stop(100%,#eaeaea));
    background: -webkit-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
    background: -o-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
    background: -ms-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
    background: linear-gradient(top, #ffffff 1%,#eaeaea 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#eaeaea',GradientType=0 );
    box-shadow: 
        0px 0px 0px 1px rgba(155,155,155,0.3), 
        1px 0px 0px 0px rgba(255,255,255,0.9) inset, 
        0px 2px 2px rgba(0,0,0,0.1);
}
.ac-container label:hover{
    background: #fff;
}
.ac-container input:checked + label,
.ac-container input:checked + label:hover{
    background: #c6e1ec;
    color: #3d7489;
    text-shadow: 0px 1px 1px rgba(255,255,255, 0.6);
    box-shadow: 
        0px 0px 0px 1px rgba(155,155,155,0.3), 
        0px 2px 2px rgba(0,0,0,0.1);
}
.ac-container label:hover:after,
.ac-container input:checked + label:hover:after{
    content: '';
    position: absolute;
    width: 24px;
    height: 24px;
    right: 13px;
    top: 7px;
    background: transparent url(../images/arrow_down.png) no-repeat center center;  
}
.ac-container input:checked + label:hover:after{
    background-image: url(../images/arrow_up.png);
}
.ac-container input{
    display: none;
}
.ac-container article{
    background: rgba(255, 255, 255, 0.5);
    margin-top: -1px;
    overflow: hidden;
    height: 0px;
    position: relative;
    z-index: 10;
    -webkit-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
    -moz-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
    -o-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
    -ms-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
    transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
}
.ac-container article p{
    font-style: italic;
    color: #777;
    line-height: 23px;
    font-size: 14px;
    padding: 20px;
    text-shadow: 1px 1px 1px rgba(255,255,255,0.8);
}

.ac-container input:checked ~ article.ac-small{
    height: 140px;
}
.ac-container input:checked ~ article.ac-medium{
    height: 180px;
}
.ac-container input:checked ~ article.ac-large{
    height: 230px;
}

注意:如果需要,我可以使用 SASS

4

1 回答 1

1

你如何实现它的答案是:不,你不能只用 CSS 来完成这个

然而,如果你改变了你的标记和显示,你可能能够以你想要的方式定位事物,尽管你可能不得不改变你的样式。无论如何,现在你有:

div-1
  input-1
  article-1
div-2
  input-2
  article-2
...

检查第二个输入时,您无法定位第一篇文章,因为您无法遍历出div-2并且即使可以,也无法遍历 fromdiv-2div-1

话虽如此,如果您真的想尝试,您可以拉出 div,反转标记和显示顺序(并使用浮动或文本方向反转它们的样式)。然后,你可以这样做:

input-4
article-4
input-3
article-3
....

您可以使用浮点数或文本方向设置样式以仍然明显地显示 1、2、3、4。因为标记是 4,3,2,1,所以理论上,如果选中较高的数字,您可以定位较低的数字,例如:

input:checked ~ article {/*show*/}

使用此规则,如果检查第四个输入(同样,它可以显示在右侧,即使它在标记中是第一个),它将显示所有文章。此外,如果选中 3(而未选中 4),则会显示 3,2,1 等。

于 2013-06-26T18:22:32.293 回答