Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Div Expand Smoothly

Tags:

javascript

I'm using Javascript to expand a div, but when the content is expanded it's highly abrupt and at best highly unattracive. I was wondering if there was a way to make this div expand slower and more of a visual expand then a BLAM. Now I'm done.

<script language="javascript"> 
function toggle_profile() {
    var ele = document.getElementById("toggleProfile");
    var text = document.getElementById("displayProfile");
    if(ele.style.display == "block") {
            ele.style.display = "none";
        text.innerHTML = "View Profile";
    }
    else {
        ele.style.display = "block";
        text.innerHTML = "Hide Profile";
    }
} 
                        </script>

                      <a id="displayProfile" href="javascript:toggle_profile();">View Profile</a></p>
                      <br />
                  <div id="toggleProfile" style="display: none">
like image 793
Morgan Green Avatar asked Oct 26 '12 20:10

Morgan Green


People also ask

How do you make a div expand to fill available space?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

How do you expand a div in HTML?

Given an HTML document and the task is to make div width expand with its content using CSS. To do this, the width property is used to set the width of an element excluding padding, margin and border of element.

How do I make my div not expand?

You can simply use the CSS display property with the value inline-block to make a <div> not larger than its contents (i.e. only expand to as wide as its contents).


2 Answers

The trick is with appending and removing classes in Javascript... And attaching a CSS3 transition like this:

div {
    -webkit-transition: height .25s ease;
       -moz-transition: height .25s ease;
            transition: height .25s ease;
}

This applies a transition you can control the speed of to all your divs. Of course you can select which DOM elements to apply it to yourself.

And then the two jquery functions I'd use are

$("#object").addClass("removeThis"); //hide
$("#object").removeClass("removeThis"); //show

But as pointed out you may not use jQuery! So here!

document.getElementById("object").className = "";
document.getElementById("object").className = "removeThis";

Where "#object" is the object you are targeting, and ".removeThis" is the class you are adding and removing from the class attribute in the dom tag. Heres what it looks like in css.

#object {
    height: 200px;
    /* ignoring other CSS */
}

.removeThis {
    height: 0;
}

Assuming that you want it to slide up and down. Another trick is to use opacity, or display: none and display: block. But play around. I hope this helps!

Edit since 2012: Since you're using using JavaScript you're asking for work to be done on the main thread, you can utilize the compositor thread by animating on a transform instead. That is if you can figure out how to get away from animating the height style property.

like image 62
Jim Avatar answered Oct 02 '22 01:10

Jim


Theres really no point in re-inventing the wheel. You can do this pretty easily with Jquery's slidetoggle. Here is the page: http://api.jquery.com/slideToggle/

like image 23
Steve's a D Avatar answered Oct 02 '22 02:10

Steve's a D