views:

20

answers:

1

Hi,

I have a DIV that im trying to adjust the top margin on upon click of the trigger link using jQuery. My code is below.

The problem is, this only works one way - decreasing the negative top margin back down to 0. But on second click it doesnt increase it back up to -200px.

Anyone have any idea why?!

$('.gh-gallink').click(
    function() {
        $('.gallery_container').animate({ marginTop: "0px" }, 2000)
    },
    function() {
        $('.gallery_container').animate({ marginTop: "-200px" }, 2000);
    }
);
+1  A: 

Instead of .click() you need .toggle() for it to cycle functions when clicked, like this:

$('.gh-gallink').toggle(
  function() {
    $('.gallery_container').animate({ marginTop: "0px" }, 2000)
  },
  function() {
    $('.gallery_container').animate({ marginTop: "-200px" }, 2000);
  }
);
Nick Craver
Hi Nick, thanks for your reply - i had tried that already, and have just copied in your code again to make sure, and that doesnt work.The first click doesnt even work with toggle.Im so confused! :(
Phil
This works to bring the top-margin down to 0px: $('.gh-gallink').click(function() { $('.gallery_container').animate({ marginTop: "0px" }, 2000, function() { });I just cant seem to get it to work backwards, via click or toggle
Phil
@Phil - Take a look here, what's different about your current code? http://jsfiddle.net/nick_craver/xVkpM/
Nick Craver
@Nick - Ive updated the code on that link with my code and its broke it! Again, no idea why. I just cant see whats wrong. Only thing missing from what you had was a container really. Which is needed because the plugin which produces the gallery only works if its present on page load (usually i would just display:none the hidden div and use toggle() to show/hide it) but i cant this time as that stops the gallery loading. Hence the negative top margin and overflow:hidden container
Phil
@Phil - It's because the div is over-top of the link, if you give it a background it gets much clearer, like this: http://jsfiddle.net/nick_craver/D7qLr/ I think this is what you're after: http://jsfiddle.net/nick_craver/D7qLr/1/
Nick Craver
@Nick - Update, okay ive put my code into that site you linked me to, and this is functioning how my page is on my system... just wont go back up once its down. See here: http://jsfiddle.net/xVkpM/19/
Phil
@Nick - SUCCESS!!I had a )}; too many at the end of my JS document!!!Ugh.. thank god thats all it was. Thank you so much for all your help! Really!!!Im new to and still learning the basics of JS and at least i can say i was on the right track/lines with it and the things i had tried earlier today b4 posting here.Thank you once again, I really appreciate it, and that site is great for testing and for stuff like this site to use! Did you build it?
Phil
@Phil - It's not mine, I just really love it as well, a few of the MooTools guys are to thank for jsfiddle. As an aside make sure to accept answers to your questions...and also, welcome to SO :)
Nick Craver
thank you again!
Phil