简体   繁体   English

单击HTML扩展框

[英]HTML expanding boxes on click

I want to create three boxes with some short text and when you click on one of those boxes, it will expand on the full width of the page and displays the whole content. 我想创建三个带有短文本的框,然后单击其中一个框,它将在页面的整个宽度上展开并显示整个内容。 Then it can be closed by clicking on some kind of icon. 然后可以通过单击某种图标将其关闭。

I created a gif so you can see what I mean. 我创建了一个gif,这样您就可以明白我的意思了。 GIF GIF

I have so far the basic structure on jsfiddle 到目前为止,我已经了解了jsfiddle的基本结构

When you click on one box it every time opens the box-3 content insted of the content of the clicked box. 当您单击一个框时,它每次都会打开由单击框的内容构成的box-3内容。 Also I dont know how to close the open box. 我也不知道如何关闭打开的盒子。

Is the animation you can see in the gif possible with css or does it need javascriptl? 您可以使用css在gif中看到的动画还是需要javascriptl?

$(".row.boxes-container .box").click(function (e) {
        $(".box-content-full").addClass("open");
    });


.row.boxes-container{
    position:relative;   
}

.box:hover{
    cursor:pointer;   
}

.box-content{
    padding:30px;
    background-color:#f03939;
}

.box-content-full{
    display:none;   
    width:0%;
    transition: width 1s;
}

.box-content-full.open {
    position: absolute;
    max-width: 100%;
    width: 100%;
    z-index: 2;
    display:block;
    transition: width 1s;

}

.box-content-full.open .inner{
    padding:30px;
    margin-left:15px;
    margin-right:15px;
    background-color: red;

}


<div class="container">
        <div class="row boxes-container">

            <div class="box first">
                <div class="box-content-full">
                    <div class="inner">
                        <div class="inner-header">
                            <span class="close">x</span>
                        </div>
                        <h3>Box 1</h3>
                        <p>Lorem ipsum dolem filum</p>
                    </div>
                </div>

                <div class="col-md-4">
                    <div class="box-content">
                        <h3>Box 1</h3>
                    </div>
                </div>
            </div>

            <div class="box second">
                <div class="box-content-full">
                    <div class="inner">
                        <div class="inner-header">
                            <span class="close">x</span>
                        </div>
                        <h3>Box 2</h3>
                        <p>Lorem ipsum dolem filum</p>
                    </div>
                </div>

                <div class="col-md-4">
                    <div class="box-content">
                        <h3>Box 2</h3>
                    </div>
                </div>
            </div>

            <div class="box third">
                <div class="box-content-full">
                    <div class="inner">
                        <div class="inner-header">
                            <span class="close">x</span>
                        </div>
                        <h3>Box 3</h3>
                        <p>Lorem ipsum dolem filum</p>
                    </div>
                </div>

                <div class="col-md-4">
                    <div class="box-content">
                        <h3>Box 3</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>

I did not use your html code but tried to get a result as close as possible to your gif: 我没有使用您的html代码,但试图获得尽可能接近您的gif的结果:

 $('.box').on('click', function() { var $element = $(this); var $openElement = $('<div class="absoluteBox"></div>'); $openElement.css('left', $element.position().left); $openElement.css('right', $element.width()); $element.html($openElement); $openElement.animate({left : 0, width : $('.wrapper').width()}, 500); }); 
 .wrapper { position:relative; display:inline-block; } .box { background-color:#CC0000; width:150px; height:200px; display:inline-block; margin-right:10px; } .box:last-of-type { margin-right:0; } .absoluteBox { position:absolute; background-color:blue; height:200px; width:150px; } .box:hover { background-color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="wrapper"> <div class="box"></div> <div class="box"> </div> <div class="box"></div> </div> 

I made the opening box blue to be better noticed. 我将打开框设为蓝色,以便更好地注意到。 This has to be changed via css of course to red. 当然,必须通过CSS将其更改为红色。

Updated your fiddle: 更新了您的小提琴:

EDIT 2: 编辑2:
Now it should reset when you click the box. 现在,当您单击该框时,它应该重置。

PS Click the second box for the effect. PS单击第二个框以获得效果。
PPS The effect displays incorrectly because the page contracts automatically in JSFiddle. PPS效果显示不正确,因为页面在JSFiddle中自动收缩。 ALTHOUGH, if you try it elsewhere and it doesn't work, consider me proven wrong! 虽然如此,如果您在其他地方尝试过却无法正常工作,请认为我被证明是错误的!

EDIT: 编辑:
Updated answer to expand element that was clicked. 更新了答案以扩展单击的元素。

https://jsfiddle.net/bszv7f90/9/ https://jsfiddle.net/bszv7f90/9/

 $(".row.boxes-container .box").click(function(e) { $(this).children().each(function(i) { if ($(this).attr('class') == 'box-content-full') { $(this).toggleClass("exp"); } }); $(".box-content-full").toggleClass("open"); $(".box-content").toggleClass("clicked"); }); 
 .container { max-width: 600px; } .row.boxes-container { position: relative; } .box:hover { cursor: pointer; } .box-content { padding: 30px; background-color: #f03939; } .box-content-full { display: none; width: 0%; transition: width 1s; } .box-content { padding: 30px; background-color: #f03939; } .box-content.clicked { display: none; } .box-content-full.open { position: absolute; max-width: 0%; width: 0%; z-index: 2; display: none; transition: width 1s; } .box-content-full.open.exp { position: absolute; max-width: 100%; width: 100%; z-index: 2; display: block; transition: width 1s; } .box-content-full.open .inner { padding: 30px; margin-left: 15px; margin-right: 15px; background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="row boxes-container"> <div class="box first"> <div class="box-content-full"> <div class="inner"> <div class="inner-header"> <span class="close">x</span> </div> <h3>Box 1</h3> <p>Lorem ipsum dolem filum</p> </div> </div> <div class="col-md-4"> <div class="box-content"> <h3>Box 1</h3> </div> </div> </div> <div class="box second"> <div class="box-content-full"> <div class="inner"> <div class="inner-header"> <span class="close">x</span> </div> <h3>Box 2</h3> <p>Lorem ipsum dolem filum</p> </div> </div> <div class="col-md-4"> <div class="box-content"> <h3>Box 2</h3> </div> </div> </div> <div class="box third"> <div class="box-content-full"> <div class="inner"> <div class="inner-header"> <span class="close">x</span> </div> <h3>Box 3</h3> <p>Lorem ipsum dolem filum</p> </div> </div> <div class="col-md-4"> <div class="box-content"> <h3>Box 3</h3> </div> </div> </div> </div> </div> 

Now it should expand. 现在它应该扩展。 I preserved your code to make a quick copy-paste. 我保留了您的代码以进行快速复制粘贴。

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

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