3

Again me with my fiddle. If you click on the item in this fiddle, it will slide open to a specific height.

The fiddle shows the section with some "summary text" (like a preview to the full loaded text when you click to open it).

The intention will be, that I load the "full message" while clicking the item and then sliding it open only that long how far the content is reaching.

My first thought would be:

  • Click, loading in full text via AJAX or sth. else
  • Putting this full text into a hidden container
  • get the height of the hidden container
  • hide "summary text" container, show "full text" container
  • scroll up to the measured height
  • Profit.

Well, this would work, I guess. But my programmer's senses tell me that this is the wrong way to do it. I don't know how long the "long text" will be, so it may be just a few lines, but it can also be a whole essay.

So which method would be the best to slide that <section> open as long as it's content goes?

As the closed <section> has a fixed height, it won't be that hard to close it again.

HTML:

<section class="mbox mbox_closed" id="effect1_1">
         <h2>BUSINESS PC</h2>

        <div class="mbox_content" id="effect2_1">
             <h3>Lorem ipsum dolor sit amet, consetetur sadipscing elitr! 
                <span>sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam</span>
            </h3>

            <p>Erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
        </div>
        <hr>
        <img src="http://www.club-3d.com/tl_files/club3d/uploads/en/content/Accesories/CAC-1052/cac-1052_use_01.png" alt="Testbild">
        <!--<div class="mbox_bar"></div>-->
    </section>

CSS:

@import url(http://fonts.googleapis.com/css?family=Open+Sans);
 body, html {
    height:100%;
    padding:0;
    margin:0;
    background:#5cafff;
    font-family:'Open Sans', sans-serif;
}
.mbox {
    z-index: 1;
    margin: 2em;
    position:relative;
    background: #ff1a00;
    background: linear-gradient(to right, #ffffff 33%, #ff1a00 100%);
    border: 1px solid #fff;
    height:200px;
    clear:both;
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=90)";
    filter: alpha(opacity=90);
    -moz-opacity: 0.9;
    -khtml-opacity: 0.9;
    opacity: 0.9;
}
.mbox_closed:hover {
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
    filter: alpha(opacity=100);
    -moz-opacity: 1.0;
    -khtml-opacity: 1.0;
    opacity: 1.0;
    -moz-box-shadow:  0px 0px 12px 8px rgb(128,128,128);
    -webkit-box-shadow:  0px 0px 12px 8px rgb(128,128,128);
    box-shadow:  0px 0px 12px 8px rgb(128,128,128);
}
.mbox_active {
    z-index: 1;
    margin: 2em;
    position:relative;
    background: #ff1a00;
    background: linear-gradient(to right, #ffffff 33%, #ff1a00 100%);
    border: 1px solid #fff;
    height:200px;
    clear:both;
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
    filter: alpha(opacity=100);
    -moz-opacity: 1.0;
    -khtml-opacity: 1.0;
    opacity: 1.0;
}
.mbox * {
    padding:0;
    margin:0;
}
.mbox_content {
    z-index: 2;
    float:left;
    padding: 20px;
    background: #ffffff;
    width:60%;
    height:160px;
    overflow: hidden;
    overflow-y: hidden;
}
.mbox img {
    z-index:-1;
    position:absolute;
    right:0;
    max-height:100%;
}
.mbox hr {
    z-index: 2;
    float:left;
    width: 0;
    height: 0;
    border-style: solid;
    margin-top: 30px;
    border-width: 15px 0 15px 20px;
    border-color: transparent transparent transparent #ffffff;
}
.mbox_bar {
    z-index: 2;
    float:right;
    width: 30;
    height: 100%;
    border-style: solid;
    border-width: 0 10px 3px 3px;
    border-color: #ffffff #ffffff #ffffff #ffffff;
}
.mbox h2 {
    text-transform: uppercase;
    font-weight:normal;
    font-size:1em;
    top:-0.7em;
    left: 0.5em;
    position:absolute;
    color:#fff;
    background:#ff1a00;
    padding:0 1em;
    border:1px solid #fff;
    border-radius:1em;
}
.mbox_content h3 {
    font-size:1.4em;
    color:#666;
    border-left:2px solid #ccc;
    padding-left: 1em;
    margin-bottom:0.5em;
}
.mbox_content h3 span {
    margin-top:0.5em;
    display:block;
    font-size:0.7em;
    text-transform: uppercase;
    font-weight:normal;
}

JS:

var state = []

$(function() {

        state[1] = true;

        $( "#effect1_1" ).click(function() {

            if ( state[1] ) {
                $( "#effect1_1" ).animate({
                    height: 600
                }, 1000 );
                $( "#effect2_1" ).animate({
                    height: 560
                }, 1000 );

                $( "#effect1_1" ).removeClass( "mbox_closed" ).addClass( "mbox_active" );

            } else {
                $( "#effect1_1" ).animate({
                    height: 200
                }, 1000 );
                $( "#effect2_1" ).animate({
                    height: 160
                }, 1000 );

                $( "#effect1_1" ).removeClass( "mbox_active" ).addClass( "mbox_closed" );

            }
        state[1] = !state[1];
        });
});
Trollwut
  • 541
  • 1
  • 7
  • 23
  • Two quick clarifying question: The text shown is a "preview" and there is more text hidden? When you click on the image you want the full text to show while having the image be larger? – Mathias Rechtzigel Apr 14 '15 at 15:05
  • I will clarify my question! Don't mind the image, I already "fixed" it. Well while being closed you see a "summary text", just a few lines to attract the reader. If you click on it, the section will slide open, revealing the full text. – Trollwut Apr 14 '15 at 15:14
  • This may be relevant: http://stackoverflow.com/a/8331169/520857 – Populus Apr 14 '15 at 15:28
  • Oh this looks very nice! Does this transition and max-height also work with jQuery? – Trollwut Apr 14 '15 at 15:30
  • 1
    Yep - the wonky thing with max-height is that the transition may be different depending on the height of the container. Here's an example with just opacity and no max-height. I'm working on an example that will grab the height of each element and set that as the height that your container opens to https://jsfiddle.net/MathiasaurusRex/zk755cr9/20/ – Mathias Rechtzigel Apr 14 '15 at 15:32
  • Hey Rex! Yes, this is the issue I can't handle. Did you come any further? – Trollwut Apr 15 '15 at 12:56

1 Answers1

3

When animating, use scrollHeight property:

Height of the scroll view of an element; it includes the element padding but not its margin.

So your JS code would be like this:

var state = []

$(function() {

        state[1] = true;

        $( "#effect1_1" ).click(function() {

            if ( state[1] ) {
                $( "#effect1_1" ).animate({
                    height: $( "#effect2_1" ).prop( "scrollHeight" )
                }, 1000 );
                $( "#effect2_1" ).animate({ // since scrollHeight brings the padding too, we need to remove it so the div not 'pops out' the container
                    height: $( "#effect2_1" ).prop( "scrollHeight" ) - parseInt( $( "#effect2_1" ).css( "padding-top" ), 10 ) - parseInt( $( "#effect2_1" ).css( "padding-bottom" ), 10 )
                }, 1000 );

                $( "#effect1_1" ).removeClass( "mbox_closed" ).addClass( "mbox_active" );

            } else {
                $( "#effect1_1" ).animate({
                    height: 200
                }, 1000 );
                $( "#effect2_1" ).animate({
                    height: 160
                }, 1000 );

                $( "#effect1_1" ).removeClass( "mbox_active" ).addClass( "mbox_closed" );

            }
        state[1] = !state[1];
        });
});

Updated fiddle.

Denis Ali
  • 1,062
  • 7
  • 8
  • Exactly how I want it, this is it! Unfortunetally, it doesn't work on IE11. It just won't do anything. Do you have an idea why? (FF, Chrome and Opera are fine.) – Trollwut Apr 22 '15 at 14:37
  • @Trollwut - Isn't IE such a b****! I'm on mac so I don't have IE right now. Try this [fiddle](https://jsfiddle.net/0atcxc43/1/). If it works, i'll update the answer. – Denis Ali Apr 22 '15 at 15:16
  • True words. Unfortunatelly it doesn't work, still doing nothing. :( (Whereby this is a feature since the launch of IE, I guess.) – Trollwut Apr 22 '15 at 15:56
  • @Trollwut - Ok, found the problem. The combination IE + JsFiddle + jQUery 1.X generates a bug on iframe of JsFiddle for IE... On my original Fiddle, if you change the version of jQuery to 2.1.3 it will work on IE. – Denis Ali Apr 23 '15 at 02:11
  • Yessir, new jQuery version and it works on all common browsers. Awesome work! Take your 50 reputation and leave the city in the morning. – Trollwut Apr 28 '15 at 09:22