When you press the div.help button (height = 0), it "leaves" down; when you press the same button, it collapses. I decided to combine all this in one cycle. Well, diva leaves, but when you click again he leaves the starting position! For some reason, in the closure height is not saved, but takes the initial value.
Here is:
someButton.onclick = showHelp; var display = document.getElementsByClassName("help")[0]; var height = 0; // начальное значение 0, такое же прописано и в css function showHelp(){ // делаем хитрость: изначально h1=0, h2=150, step=1, // при повторном клике значения меняются! - h1=150, h2=0, step=-1 // цикл в каждом из случаев то наращивается до 150, то снова опускается до 0 var h1,h2,step; h1 = height; h2 = height = Math.abs(height*1 - 150); step = (h2-h1)/Math.abs(h2-h1); console.log(h1,h2,step); // проверяем. Действительно это работает for (i = h1; i != h2 ; i = i + step){ (function(){ var ii = i;// замыкаем значение каждого из i setTimeout(function(){ display.style.height = ii + "px"; },(ii+1)*5); })(); } } Everything is almost perfect. The closure works correctly. ii takes values from 0 to 150, then from 150 to 0. However, display.style.height somehow increases from 0 to 150 each time, and that's it. Maybe I missed some trifle? I do not understand anything. After all, style.height can not be some local property ...