Jump to content

jQuery toggle happens only one time


Cyto

Recommended Posts

Hey,

 

I have this code:

<script type="text/javascript">
$(document).ready(function(){
$('.rij2_content_fav').hide();
$('.mijngames').click(function() {
$('.rij2_content_fav').slideToggle('slow', function() {
$('.rij2_content_fav').animate({left: "-=232px", top: "+=50px"});
});
});
});
</script>

 

The code works, does his job, but it toggles only one time. I need to refresh page to toggle again. How can i fix this, so that it toggles when i click several times?

 

Cheers.

Link to comment
https://forums.phpfreaks.com/topic/219152-jquery-toggle-happens-only-one-time/
Share on other sites

At face value and without seeing the context of this code, I'd say that every time you click, it "slides down" on first click, "slides up" on 2nd, etc... (because slideToggle() works by the current visibility state of the object is)...but then you have that animate() in there.  EVERY time you click, it moves your item to the right 232px and down 50px. 

 

I think what you probably need to do is instead of using .click(), use .toggle() and make the first function be what you have now, and the 2nd one be the same thing, only reverse the order of slideToggle() and animate() and also reverse the animation properties. Something like...

 

<script type='text/javascript'>
$(document).ready(function() {

$('.rij2_content_fav').hide();

$('.mijngames').toggle(
  function() {
    $('.rij2_content_fav').slideToggle('slow', function() {
      $('.rij2_content_fav').animate(
        { 
          left : '-=232px', 
          top: '+=50px' 
        }
      );
    });
  },
  function() {
    $('.rij2_content_fav').animate(
      { 
        left : '+=232px', 
        top: '-=50px' 
      },
      function () {
        $('.rij2_content_fav').slideToggle('slow');
      }
    );
  }
);

});
</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.