Unbinding/Rebinding .on()/.off() mouse events from animated buttons

放肆的年华 提交于 2019-12-13 08:35:38

问题


This question is somehow related to one other I posted before. The reason I'm making a new question is because I changed things a bit and haven't been able to deal with a litte issue since then.

So, I have this simple horizontal slideshow. I have a left and right button to control it (besides the ability to directly click on the slides). Basically I want my buttons to deactivate themselves when the slideshow reaches it's left/right end and reactivate when they are nedded again. I've tried with .off() but it just deactivates permanentely. When I want it back on - .on() - it doesn't work.

HTML markup:

<div id="rocksMenu_btnLeft"></div>
<div id="rocksMenu_btnRight"></div>

      <div id="centerArea">
         <div id="menuContainer">
            <div id="menuItem_1" class="menuItem">
               <div class="content">Contents1</div>
            </div>
            <div id="menuItem_2" class="menuItem">
               <div class="content">Contents2</div>
            </div>
            <div id="menuItem_3" class="menuItem">
               <div class="content">Contents3</div>
            </div>
            <div id="menuItem_4" class="menuItem">
               <div class="content">Contents4</div>
            </div>
         </div>
      </div>

Full code here - Fiddle (clean version - I deleted my button disabling code try, because it was getting messy). I've searched for this issue, but couldn't get a clear answer.

Thanx.

Pedro


回答1:


You don't have to deactivate the event, just do a check and if the end is reached, don't do the animation action.

$('#rocksMenu_btnRight').on('click', function() {
    if (menuItem_place === menuItem_limit) return;
    // animation code
}

For btnLeft just change the condition if (menuItem_place === 0) return;.



来源:https://stackoverflow.com/questions/16019825/unbinding-rebinding-on-off-mouse-events-from-animated-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!