Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change div height onclick with animation

I'm trying to make a gallery using divs that change their height when you click on them. Ideally, this would include animation to smoothly expand the div's height. There will be several of each div on each page, so it needs to just expand that section.

It's actually supposed to turn out something like the news section on this page: http://runescape.com/

I'd like to do it with JavaScript/jQuery if possible.

like image 943
JacobTheDev Avatar asked Apr 03 '11 17:04

JacobTheDev


4 Answers

$('div').click(function(){
    $(this).animate({height:'300'})
})

Check working example at http://jsfiddle.net/tJugd/

like image 164
Hussein Avatar answered Sep 21 '22 14:09

Hussein


Here's the code I ended up using:

JS:

document.getElementById("box").addEventListener("click", function() {
    this.classList.toggle("is-active");
});

CSS:

#box {
    background: red;
    height: 100px;
    transition: height 300ms;
    width: 100px;
}

#box.is-active {
    height: 300px;
}

HTML:

<div id="box"></div>

Fiddle:

https://jsfiddle.net/cp7uf8fg/

like image 36
JacobTheDev Avatar answered Sep 22 '22 14:09

JacobTheDev


try

$('div').toggle(function(){
    $(this).animate({'height': '100px'}, 100);
}, function(){
    $(this).animate({'height': '80px'}, 100);
});

DEMO

like image 2
qwertymk Avatar answered Sep 22 '22 14:09

qwertymk


jQuery rules. Check this out.

http://api.jquery.com/resize/

like image 1
Trevor Arjeski Avatar answered Sep 22 '22 14:09

Trevor Arjeski