【问题标题】:Expand and collapse with css用 css 展开和折叠
【发布时间】:2014-11-25 02:22:56
【问题描述】:

我在 Jquery 中创建了一个函数来展开和折叠 div 的内容。但是现在我想只使用 CSS 来制作它,并使用箭头的图像,比如这些

观看直播jsFiddle

我也想消除所有这些标签跨度,只保留 div 和它的内容

这是我到目前为止的代码。

<div class='showHide'>
    <span class='expand'><span id="changeArrow">&#8593;</span>Line expand and collapse</span>
    <fieldset id="fdstLorem">Lorem ipsum...</fieldset>
</div>
$(document).ready(function () {
    $('.showHide>span').click(function () {
        $(this).next().slideToggle("slow");
        return false;
    });
    $(".showHide>span").toggle(function () {
        $(this).children("#changeArrow").text("↓");
    }, function () {
        $(this).children("#changeArrow").text("↑");
    });
});

【问题讨论】:

标签: css collapse expand


【解决方案1】:

.fieldsetContainer {
    height: 0;
    overflow: hidden;
    transition: height 400ms linear;
}

#toggle {
    display: none;
}

#toggle:checked ~ .fieldsetContainer {
    height: 50px;
}

label .arrow-dn { display: inline-block; }
label .arrow-up { display: none; }

#toggle:checked ~ label .arrow-dn { display: none; }
#toggle:checked ~ label .arrow-up { display: inline-block; }
<div class='showHide'>
    <input type="checkbox" id="toggle" />
    
    <label for="toggle">
        <span class='expand'>
            <span class="changeArrow arrow-up">↑</span>
            <span class="changeArrow arrow-dn">↓</span>
            Line expand and collapse
        </span>
    </label>
    
    <div class="fieldsetContainer">
        <fieldset id="fdstLorem">
            Lorem ipsum...
        </fieldset>
    </div>
</div>

【讨论】:

  • 是否有一些巧妙的方法可以让每页不止一个可折叠区域?
【解决方案2】:

这是我的做法,与使用复选框做的事情相比,它简单明了,但只会在悬停或单击并按住时展开(这对触摸有好处)...

<style>

.collapse-able {
    height: 1.2rem;
    overflow: hidden;
}

.collapse-able:active, .collapse-able:hover {
    height: auto;
}

</style>

<div class="collapse-able">
  <h1> The first line will always show </h1>
  </br> 
  But what's underneath won't until hover or click and drag. Which is neat.
</div>

【讨论】:

    猜你喜欢
    • 2015-04-19
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 2018-12-13
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    相关资源
    最近更新 更多