John Abraham
John Abraham

Reputation: 18811

How do I unbind and rebind on() click animations?

I have a recursive loop that does a simple animation. These animations are controlled by on page load & onclick of controls .carousel_item.

LIVE SAMPLE JSFIDDLES CLICK HERE stripped down for simplicity purposes

Problem: When Repeat clicking on the same control the animation will fire over and over. I would like the animation to only fire once per carousel_item.

UPDATEI would like the animation to only fire once per carousel_item. And reset when the following carousel_item is clicked.

EXAMPLE: Pushing 1 repeatedly will trigger the animation over and over again. I would like the first press to trigger the animation once. Next User mashes 2 this will retrigger the animation once and not repeatedly. User can still push 1 again for the mashing is all I want to prevent

Logically, I see the click unbinding itself and rebinding itself on click of another .carousel_item that's my logic; however, I'm sure there's a better solution

var carItems = $('.carousel_item');
var sideitems = $('.side_item');
var x = false;
$(sideitems).hide();
fadeItem();

$(carItems).on({
    click: function () {

        $(sideitems).stop(true, true).animate({
            right: '4.5em'
        });
        $(sideitems).hide();

        fadeItem();

    },
    mouseenter: function () {

    },
    mouseleave: function () {

    }
});


function fadeItem() {
    $('.side_ul li:hidden:first').fadeIn(fadeItem).animate({
        right: '-4.5em'
    }, 150, function () {});;
}​

HTML

<div id="carousel" class="flexslider">
      <ul class="slides">
        <li class="carousel_item"> <img src="asset/img/1600/slide1_1600.jpg" /> </li>
        <li class="carousel_item"> <img src="asset/img/1600/slide2_1600.jpg" /> </li>
        <li class="carousel_item"> <img src="asset/img/1600/slide1_1600.jpg" /> </li>
        <li class="carousel_item"> <img src="asset/img/1600/slide2_1600.jpg" /> </li>
        <!-- items mirrored twice, total of 12 -->
      </ul>
    </div>

      <nav class="side_nav">
    <ul class="side_ul">
        <li class="side_item home"><div class="text_links"><a href="#">home</a></div></li>
        <li class="side_item document"><div class="text_links"><a href="#">docs</a></div></li>
        <li class="side_item video"><div class="text_links"><a href="#">video</a></div></li>        
        <li class="side_item programming"><div class="text_links"><a href="#">web</a></div></li>

    </ul>
  </nav>

enter image description here LIVE SAMPLE JSFIDDLES CLICK HERE

Upvotes: 2

Views: 589

Answers (1)

Tats_innit
Tats_innit

Reputation: 34117

Working Demo http://jsfiddle.net/ScbDG/2/

You can use .off after click event; http://api.jquery.com/off/ & $(this) will make sure that its set to off for that particular object.

Good read: Best way to remove an event handler in jQuery?

Hope this helps, :)

code

$(carItems).on({
     click: function () {

         $(sideitems).stop(true, true).animate({
             right: '4.5em'
         });
         $(sideitems).hide();

         fadeItem();
         $(this).off('click');

     },
     mouseenter: function () {

     },
     mouseleave: function () {

     }



 });

Upvotes: 3

Related Questions