javascript - Can't get jQuery .off() to remove event handler -
i can't seem figure out why .off('click') jquery method isn't working considering removing event binding '.reveal-menu'. first animation works fine each div moves respective location in increments of 78. can't seem go back.
jquery
$(function(){ var usermenudivs = $('#user-menu div:gt(1)'); usermenudivs.hide(); $('.reveal-menu').on('click', function(){ $(this).addclass('down'); iconspacercount = 78; usermenudivs.show(); $.each(usermenudivs, function(index,value){ $(this).animate({top:iconspacercount},200); iconspacercount += 78;}); }); $('.reveal-menu').off('click', function(){ $(this).removeclass('down'); iconspacercount = 0; $.each(usermenudivs, function(index,value){ $(this).animate({top:iconspacercount}); }); }); });
html
<div id="user-menu"> <div class="reveal-menu"> <i class="fa fa-plus fa-lg"></i> </div> <div class="post-option"> <a href="#" class="fa fa-pencil fa-2x"></a> </div> <div class="search-option"> <a href="#" class="fa fa-search fa-lg"></a> </div> <div class="chat-option"> <a href="#" class="fa fa-comments fa-lg"></a> </div> <div class="profile-option"> <a href="#" class="fa fa-user fa-lg"></a> </div> <div class="friends-option"> <a href="#" class="fa fa-users fa-lg"></a> </div> <div class="organizations-option"> <a href="#" class="fa fa-building-o fa-lg"></a> </div> <div class="public-figures-option"> <a href="#" class="fa fa-bullhorn fa-lg"></a> </div> <div class="sign-out-option"> <a href="#" class="fa fa-sign-out fa-lg"></a> </div> </div>
here link video show i'm doing. https://dl.dropboxusercontent.com/u/3270373/2016-06-01_21-57-20.mp4
the .off
used remove events. not used add opposite event of .on
. can switch click
event between 2 function this:
$(function() { var usermenudivs = $('#user-menu div:gt(1)'); usermenudivs.hide(); $('.reveal-menu').on('click', revealmenuon); }); function revealmenuon() { $(this).addclass('down'); $('#user-menu div:gt(1)').slidedown( "slow" ); $('.reveal-menu').off('click').on('click', revealmenuoff); } function revealmenuoff() { $(this).removeclass('down'); $('#user-menu div:gt(1)').slideup( "slow" ); $('.reveal-menu').off('click').on('click', revealmenuon); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="user-menu"> <div class="reveal-menu"> <i class="fa fa-plus fa-lg">1</i> </div> <div class="post-option"> <a href="#" class="fa fa-pencil fa-2x">2</a> </div> <div class="search-option"> <a href="#" class="fa fa-search fa-lg">3</a> </div> <div class="chat-option"> <a href="#" class="fa fa-comments fa-lg">4</a> </div> <div class="profile-option"> <a href="#" class="fa fa-user fa-lg">5</a> </div> <div class="friends-option"> <a href="#" class="fa fa-users fa-lg">6</a> </div> <div class="organizations-option"> <a href="#" class="fa fa-building-o fa-lg">7</a> </div> <div class="public-figures-option"> <a href="#" class="fa fa-bullhorn fa-lg">8</a> </div> <div class="sign-out-option"> <a href="#" class="fa fa-sign-out fa-lg">9</a> </div> </div>