Hellodan
Hellodan

Reputation: 1208

How can I animate my div to slide out on button click

Im having a problem, I have managed to get some code to make a div element slide up into view but how would I get it to slide down again if the close button has been clicked? so that I can keep pressing the link and it will play the animation again rather then just showing the div already in its final position.

heres the jquery code

$(document).ready(function() { 

//select all the a tag with name equal to modal
$('a[name=modal]').click(function(e) {
    //Cancel the link behavior
    e.preventDefault();
    //Get the A tag
    var id = $(this).attr('href');

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set height and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});

    //transition effect    
    $('#mask').fadeIn(600);   

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    //Set the popup window to center
    $(id).css('top',  winH/2-$(id).height()/2);
    $(id).css('left', winW/2-$(id).width()/2);

    //Slide up Boxes Div transition effect
    $(id).css({display:'block'}).animate({marginTop:'0px', opacity:'1', },400);

});

//if close button is clicked
$('.window .close').click(function (e) {
    //Cancel the link behavior
    e.preventDefault();
    $('#mask, .window').hide();
});    

//if mask is clicked
$('#mask').click(function () {
    $(this).hide();
    $('.window').hide();
});        

});

thanks

Upvotes: 0

Views: 3971

Answers (1)

Jasper
Jasper

Reputation: 75993

You can just do the opposite of what you are doing to slide-up the element:

$('.window .close').click(function (e) {
    //Cancel the link behavior
    e.preventDefault();
    $(this).closest('.window').animate({
        marginTop : 1200,
        opacity   : 0
    }, 400, function () {
        $(this).css('display', 'none');
    });
}); 

There are a few errors in your code:

  1. .animate({marginTop:'0px', opacity:'1', },400) has a trailing comma that will force errors in Internet Explorer, it should be: .animate({marginTop:'0px', opacity:'1' },400).
  2. You call .css twice in a row on the same element, you can just pass an object to the .css function like you do further down in your code:

.

$(id).css({
    top     : winH/2-$(id).height()/2,
    left    : winW/2-$(id).width()/2,
    display : 'block'
}).animate({marginTop:'0px', opacity:'1' },400);

Notice I also chained the .animate() function call, this way you only select $(id) once.

.slideUp() and .slideDown()

Are you aware that jQuery already has functions that do this?

UPDATE

Here is an updated version of your jsfiddle: http://jsfiddle.net/4hw8H/1/

$(function() { 

    $('a[name=modal]').click(function(e) {
        e.preventDefault();
        
        var $this      = $($(this).attr('href')),
            winH       = $(window).height(),
            winW       = $(window).width(),
            maskHeight = $(document).height(),
            maskWidth  = $(window).width();
    
        $('#mask').css({
            width  : maskWidth,
            height : maskHeight
        }).fadeIn(600);   
    
        $this.css({
            top     : winH / 2 - $this.height() / 2,
            left    : winW / 2 - $this.width() / 2,
            display : 'block'
        }).animate({
            marginTop : 0,
            opacity   : 1
        }, 400);
    
    });
    
    $('.window .close').click(function (e) {
        e.preventDefault();
        $(this).closest('.window').animate({
            marginTop : 1200,
            opacity   : 0
        }, 400, function () {
            $(this).css('display', 'none');
        });
        $('#mask').fadeOut(600);
    });    
    
    $('#mask').click(function () {
        $('.window .close').trigger('click');
    });        

});

Upvotes: 2

Related Questions