1

Here is my jfiddle: http://jsfiddle.net/6mfb1kL0/

HTML

<div id="SearchDiv">
            Hello there...
</div>


 <button type="button" onClick="changeHeight();">Click Me!</button>

CSS

#SearchDiv{
 background-color:red;   
 width:1000px;
 height:20px;
}

JS

function changeHeight() {
document.getElementById('SearchDiv').style.height = "200px";
}
Sherri
  • 307
  • 2
  • 6
  • 14

4 Answers4

5

you can use the animate method in jquery for the desired behavior. here 200 is the height in pixels and 500 is the time in milliseconds.

   function changeHeight() {

        $('#SearchDiv').animate({height:200},500);
    }
Sushil
  • 2,837
  • 4
  • 21
  • 29
3

CSS3

You can use CSS3 transitions, this leaves your JavaScript alone. It will also automatically make all height changes smooth.

-webkit-transition: 2s height ease;
transition: 2s height ease;

Your Fiddle

Support Table


jQuery

You can also use jQuery .animate()

$('#SearchDiv').animate({
    'height': '200px'
}, 500);

Fiddle

jQuery Browser Support

Downgoat
  • 13,771
  • 5
  • 46
  • 69
2

Is there a reason why you have to do it with jQuery? It's quite easy with CSS.

jsfiddle

 transition: all .5s ease;
 -webkit-transition: all .5s ease
  • 2
    What does your answer introduce that wasn't already posted 6 minutes ago 4 times? (once deleted) – Sterling Archer Apr 10 '15 at 19:23
  • 2
    No need to add any other prefixes but `-webkit-` for older android browsers. – Downgoat Apr 10 '15 at 19:23
  • 3
    Man, a quick trip to the bathroom and I catch flack for not being the first to the table. When I clicked to answer there were 0 in here. – Jesse Dockett Apr 10 '15 at 19:26
  • 2
    @JesseDockett isn't stack overflow fun? – Dan Beaulieu Apr 10 '15 at 19:29
  • 1
    It certainly is different. I figured I had gotten enough help from the community over the last few years, I should start to give back. Really strange to get scolded for giving a valid answer shortly after a question was posed. Also, nobody else included the vendor prefix in their CSS based answers. – Jesse Dockett Apr 10 '15 at 19:33
1

You can use the animate function in jQuery: http://api.jquery.com/animate/

Or if browser compatibility is less of a concern you can do it purely through css: http://www.w3schools.com/css/css3_transitions.asp

I'd knock a sample of each up, but I'm not at a PC right now!

Steven Brookes
  • 834
  • 6
  • 27
  • 1
    Don't add links for an answer – Downgoat Apr 10 '15 at 19:17
  • 2
    why not? I believe that it helps to give links even if it's not the full answer so that a user can use them and gain the knowledge required to do what she/he needs. And even if you give the answer with code you should always give links to the documentation that you used to achieve the answer so that the user can keep learning. – Samuel Lopez Apr 10 '15 at 19:21
  • 1
    My guess I'd because the link may end up 'dead' in future, where as relevant quoted text will always be available? – Steven Brookes Apr 10 '15 at 19:37