tags:

views:

185

answers:

2

I am using a toggle on a graphic to slide out a contact form. The problem is, the contact form can cover up the graphic element on low resolutions. I thought a solution would be to include a "close this" inside the form, that would use the same toggle effect. When I add the close this element to the code, instead of working in tandem with the original graphic element, it starts the chain back over, and slides the contact form even further out.

Site is here: http://www.tritonloyaltysupport.com/status

Code for toggle here:

$(this).html(div_form); 
//show / hide function 

$('div.contactable').toggle( 
    function() { 
        $('#overlay').css({display: 'block'}); 
        $('#contactForm').animate({"marginRight": "-=0px"}, "fast");
        $('#contactForm').animate({"marginRight": "+=390px"}, "slow");
    }, 
    function() {
        $('#contactForm').animate({"marginRight": "-=390px"}, "slow");  
        $('#overlay').css({display: 'none'}); 
    }
);
A: 

You might try setting your two toggle actions as functions outside your $().toggle(); and then bind the hide function to the new panel.

function show() {
        $('#overlay').css({display: 'block'}); 
        $('#contactForm').animate({"marginRight": "-=0px"}, "fast");
        $('#contactForm').animate({"marginRight": "+=390px"}, "slow");
}

function hide() {
        $('#contactForm').animate({"marginRight": "-=390px"}, "slow");  
        $('#overlay').css({display: 'none'}); 
}

$('div.contactable').toggle(show, hide);

$('button .close', '#contactForm').click(hide);

Obviously you'll have you change that last line to target your close button, but that should get you started.

Rookwood
@Rookwood - You are passing the functions to `toggle()` and `click()` incorrectly, and your context argument should be in quotes `'#contactForm'`.
patrick dw
You are, of course, correct. This is what comes of typing in haste while thinking on other things. Corrected, and thanks.
Rookwood
@Rookwood - You're welcome, but the `click()` still needs to be corrected. Also, you don't necessarily need to call them from within another function as you did. All you need to do is assign them like this: `$('div.contactable').toggle(show,hide);` This passes a reference to the functions. The way you had it earlier, you were *calling* the functions because of the execution operator `()` after the function references.
patrick dw
Oh, it's one of those days... sheesh
Rookwood
Thanks everyone for your responses.I've implemented this, but the scenario still remains.If you click the graphic to open, it slides it in view. If you then click the close button in the form, it does close. If you go then to click the graphic to open it again, it still assumes its open and proceeds to "close" it again, moving it an additional -390px. Any thoughts?
Jesse
A: 

Guys, got the answer. I ended up reusing a previous piece of code for a slide panel, and repurposed it. Code follows for those interested:

    $(this).html(div_form);
        $("div.contactable").click( function() {
            if ($("#openCloseIdentifier").is(":hidden")) {
                $("#contactForm").animate({ 
                    marginRight: "0px"
                    }, 500 );
                $("#openCloseIdentifier").show();
            } else {
                $("#contactForm").animate({ 
                    marginRight: "-390px"
                    }, 500 );
                $("#openCloseIdentifier").hide();
            }
        });  
Jesse