1

I have a site and when someone clicks the contact link, in the nav, a contact box appears and slides down. The nav then moves down to remain below the contact box (that just appeared). The issue is when you click the contact link again the toggle hides the contact box but leaves the nav down where it would be if the contact box was still visible. Here is the code:

contactBarBtn.click(function (e) {
    search_close();
    contactBar.toggle();
    navbarFixedTop.animate({ top: contactBar.height() }, 'slow'),
    contactBar.animate({ top: '0' }, 'slow'),
    contactCorner.css('display', 'block'),
    e.preventDefault();
    return false;
});

Any ideas?

2
  • 1
    Why are the statements separated by commas ? Commented Jun 15, 2015 at 20:02
  • Not sure. That is how the script came. Commented Jun 15, 2015 at 20:28

2 Answers 2

1

I found the solution.

contactBarBtn.click(function (e) {
    if (jQuery('.contact-bar-corner').css('display') == 'block') {  
        search_close();
        navbarFixedTop.animate({ top: '0' }, 'slow');
        contactBar.animate({ top: -contactBar.height() }, 'slow');
        stickyHeader.removeClass('sticky');
        contactCorner.css('display', 'none');
        e.preventDefault();
        return false;
    }
    else if (jQuery('.contact-bar-corner').css('display') == 'none') {  
        search_close();
        contactBar.show();
        navbarFixedTop.animate({ top: contactBar.height() }, 'slow');
        contactBar.animate({ top: '0' }, 'slow');
        stickyHeader.addClass('sticky');
        contactCorner.css('display', 'block');
        e.preventDefault();
        return false;
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent -- you can probably use jQuery('.contact-bar-corner').is(':visible') instead of jQuery('.contact-bar-corner').css('display') == 'none' but that is a minor style thing. Sorry, I got a really bad cold and didn't get a chance to make a JSFiddle. I'm glad you figured it out.
I'm sorry to hear that. Thanks for the help.
0

The problem is top: contactBar.height() will run before contactBar.toggle() has finished -- in fact, it will run almost right away. So instead, you need to move that into the toggle to get called when the toggle animation is finished:

contactBar.toggle(function() {
  navbarFixedTop.animate({ top: contactBar.height() }, 'slow');
});

Also, the lines should end with ; not , as the commenter mentions!

4 Comments

It still remains there when I click it the second time. The contact box is disappearing but the nav is not returning to the top. I tried adding a second function and the same code but adding a -contactBar.height() but then the link stopped working.
Here is the code contactBar.toggle(function(){ navbarFixedTop.animate({ top: contactBar.height() }, 'slow'), contactBar.animate({ top: '0' }, 'slow'), stickyHeader.addClass('sticky'), contactCorner.css('display', 'block'), },function(){ navbarFixedTop.animate({ top: -contactBar.height() }, 'slow'), contactBar.animate({ top: '0' }, 'slow'), stickyHeader.removeClass('sticky'), contactCorner.css('display', 'none'), });
@JosephManning Hrm, I think we need a JSFiddle as there is too much going on -- I suspect you need to keep track of the original top value and put that back in when toggle is closing.
The js has a close option of function contact_close() { contactBar.animate({ top: -contactBar.height() }, 'slow'); contactCorner.css('display', 'none'); } I apologize I've never used jsfiddle and am having a hard time getting this site to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.