简体   繁体   English

使用切换折叠/展开内容框

[英]Collapse/Expand a content box with a toggle

I go right to the point, I have a few boxes that I want them to be expanded and collapsed with a toggle located in their headers. 我直截了当地说,我有几个盒子,我希望它们可以扩展和折叠,并在其标题中设置一个切换。 This toggle is an anchor tag which has a sprite background, the top part of this sprite image is pointing at top, and bottom section is pointing at down. 此切换是一个具有精灵背景的锚标记,此精灵图像的顶部指向顶部,底部指向下方。 You can guess what they mean and I want their state to be changed (first one for collapse and second one for expand) 你可以猜出他们的意思,我希望他们的状态能够被改变(第一个用于崩溃,第二个用于扩展)

The structure of the boxes are something like this : 盒子的结构是这样的:

     <section class="box2">
        <header class="box-head">
            <div class="box-title fr">              
                <span class="box-icon"></span>
                <h3 class="box-title-text">Title Title</h3>
            </div>

            <a class="box-toggle fl active" href="#">A</a>
            <br class="cfx" />
        </header>

            <div class="box-content">
                <img src="img/chart-1.png" alt="" />
                                    //Content or collapsing data goes here
            </div>

    </section>

And I used one of the most straight forward ways to achieve this effect. 而且我使用了一种最直接的方法来实现这种效果。 You can see the following CSS and jQuery code below. 您可以在下面看到以下CSS和jQuery代码。 (I chose active class as default when the icon is pointing at top) (当图标指向顶部时,我选择活动类作为默认值)

JS : JS:

 <script type="text/javascript">
        $("a.box-toggle").toggle(function(){
        $(this).addClass("active");
        }, function () {
        $(this).removeClass("active");
    });

    //Slide up and down on click
    $("a.box-toggle").click(function(){
        $(this).next("div.box-content").slideToggle("slow");
        });
    });
    </script>

CSS : CSS:

.widget-toggle { 
    display: block;
    overflow: hidden;
    text-indent: -9999px;
    width: 18px; height: 9px;
    margin-top: 15px;
    margin-left: 13px;
    background: url(../img/sidebar-arrows.png) no-repeat 0 -18px;
}

.widget-toggle.active { 
    background: url(../img/sidebar-arrows.png) no-repeat 0 0;
}

Thanks for your huge help :) 谢谢你的巨大帮助:)

Edit No. #1 : 编辑编号#1:

Thanks to @Recode , their tip worked just fine, But according to what I explained and you can see in this picture. 感谢@Recode,他们的提示工作得很好,但根据我的解释,你可以在这张图片中看到。 I wanna show the state of this with an Icon 我想用一个Icon来展示它的状态 在此输入图像描述

Active is pointing at top and Inactive is pointing at bottom, when I want the box to be collapsed I'm showing "Active" and when I want the box to be expanded I'm showing "Inactive" . Active指向顶部,Inactive指向底部,当我想要折叠框时我显示“活动”,当我想要扩展框时我显示“非活动”。 With this code I managed to show active at default (I set the class of each box to active manually, if there is a better way to set the default class to active or whatever else please note.) When I click on it, box collapses and the Icon transitions to Inactive state. 使用此代码我设法在默认情况下显示活动(我将每个框的类设置为手动激活,如果有更好的方法将默认类设置为活动或其他任何请注意。)当我点击它时,框折叠并且图标转换为非活动状态。 And When I click again box expands but the Icon stays in the same state (Inactive and pointing at bottom). 当我再次单击框展开但图标保持相同状态(非活动并指向底部)。

And after clicking : 点击后: 在此输入图像描述

Here is the Code : 这是代码:

$("a.box-toggle").click(function(){
    $(this).addClass("active");
    }, function () {
    $(this).addClass("inactive");
});

Thanks a lot, Again. 非常感谢,再次。

Just use this: 只要用这个:

$(document).ready(function() {
    //Slide up and down on click
    $("a.box-toggle").click(function(){
        $(this).toggleClass('inactive');
        $(this).parent().next("div.box-content").slideToggle("slow");
    });
});

http://jsfiddle.net/Recode/DLxaB/ http://jsfiddle.net/Recode/DLxaB/

updated fiddle: http://jsfiddle.net/Recode/DLxaB/1/ 更新小提琴: http//jsfiddle.net/Recode/DLxaB/1/

Try this: FIddle 试试这个: 飞过

jQuery code: jQuery代码:

$("a.box-toggle").on('click', function () {
    $('div.box-content').slideToggle(200).toggleClass('active');
});

.slideToggle() .toggleClass() .slideToggle() .toggleClass()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM