James Pale
James Pale

Reputation: 185

javascript, slide div down on button click?

i am trying to get a div to slide down about 200px down a page using javascript when the user clicks another div which acts as a close button.

can someone please show me how i would do this?

<script>
$('.chat_close').click(function(e){
        $('.chat_box').slideDown();
    });

</script>

Upvotes: 0

Views: 1657

Answers (2)

What have you tried
What have you tried

Reputation: 11148

Use animate

Also, use .on

<script type="text/javascript">
$('.chat_close').on('click', function(e){
        $('.chat_box').animate({"top": "200px");
    });

</script>

HTML:

<div class="chat_close">
</div>

Upvotes: 0

Alessandro Incarnati
Alessandro Incarnati

Reputation: 7246

Someone already asked the same question a while ago. Avinash replied:

var minheight = 20;
var maxheight = 100;
var time = 1000;
var timer = null;
var toggled = false;

window.onload = function() {
    var controler = document.getElementById('slide');
    var slider = document.getElementById('slider');
    slider.style.height = minheight + 'px'; //not so imp,just for my example
    controler.onclick = function() {  
        clearInterval(timer);
        var instanceheight = parseInt(slider.style.height);  // Current height
        var init = (new Date()).getTime(); //start time
        var height = (toggled = !toggled) ? maxheight: minheight; //if toggled

        var disp = height - parseInt(slider.style.height);
        timer = setInterval(function() {
            var instance = (new Date()).getTime() - init; //animating time
            if(instance <= time ) { //0 -> time seconds
                var pos = instanceheight + Math.floor(disp * instance / time);
                slider.style.height =  pos + 'px';
            }else {
                slider.style.height = height + 'px'; //safety side ^^
                clearInterval(timer);
            }
        },1);
    };
};

You can see it here at this URL:

http://jsbin.com/azewi5

Upvotes: 1

Related Questions