jQuery Simple Collapsible Div?

前端 未结 4 1857
南笙
南笙 2021-01-31 17:57

I am looking for the proper, simple, small code to do the following things:

  • Click on Element with Class Applied to it.

  • DIV.CLASS - Which expands

相关标签:
4条回答
  • 2021-01-31 18:16

    Bad idea to use accordion. Better is to create your own collapsible block.

    Example:

    function InitSpoilBlock(idClicked)
        {
            $(idClicked).on('click', function(e){
                var textArray = ['blind','slide'];//here you can add other effects
    
                var randomEffect = textArray[Math.floor(Math.random()*textArray.length)];
    
                $(e.target).parent().children(".HiderPanel").toggle(randomEffect);
            });
        }
    

    so when you write such html:

    <div class="HiderContainer">
        <a href="#" class="Hider">More</a>
        <div class="HiderPanel">
            Spoiled block of html
        </div>
    </div>
    

    and after page load you will call

    InitSpoilBlock('.Hider');
    

    all blocks will be possible to collapse and hide with random animation. Or you can use one exact animation also.

    0 讨论(0)
  • 2021-01-31 18:28

    I wanted to do this with multiple divs, each with their own trigger. Building on AlienWebguy's answer above:

    HTML

    <div>
        <p class="expand" id="expand-1">more 1...</p>
    </div>
    <div class="expandable" id="expandable-1">
        <p>1. This is the content that was hidden before, but now is... Well, visible!"</p>
    </div>
    <div>
        <p class="expand" id="expand-2">more 2...</p>
    </div>
    <div class="expandable" id="expandable-2">
        <p>2. This is the content that was hidden before, but now is... Well, visible!"</p>
    </div>
    

    Javascript

    $('.expand').click(function(){
        target_num = $(this).attr('id').split('-')[1];
        content_id = '#expandable-'.concat(target_num);
        $(content_id).slideToggle('fast');
    });
    

    CSS

    .expand {
        font-weight: bold;
        font-style: italic;
        font-size: 12px;
        cursor: pointer;
    }
    .expandable {
        display:none;
    }
    div {
        margin: 10px;
    }
    

    https://jsfiddle.net/Q4PUw/3767/

    0 讨论(0)
  • 2021-01-31 18:29

    I was looking at this and wanted a collapsible div that was already styled for me. Then I realized what I wanted was a single pane jquery-ui accordion.

    <div id="collapse">
       <h3>Collapse and Expand</h3>
       <div>Content</div>
    </div>
    <script>
    $( "#collapse" ).accordion({
        collapsible: true,
        active: false
    });
    </script>
    

    http://jsfiddle.net/MB4ch/1/

    0 讨论(0)
  • 2021-01-31 18:37
    $('.expand-one').click(function(){
        $('.content-one').slideToggle('slow');
    });
    

    Demo: http://jsfiddle.net/Q4PUw/2/

    0 讨论(0)
提交回复
热议问题