无法删除 touchmove 事件监听器
Can not remove touchmove eventlisteners
我有这个 jquery 代码,我试图在用户单击汉堡包图标并且导航菜单一直下降时禁用滚动,并在他们关闭菜单时再次启用滚动.问题是,在移动版本中,我可以禁用滚动,但我无法再次启用它。即使在您关闭菜单后,window 也不会滚动。
这是 jquery 代码。
jQuery('.mobile-navigation-toggle').on("click", function() {
$('html').toggleClass('toggle_bg');
$('body').toggleClass('toggle_bg');
jQuery('.mobile_menu_wrapper').slideToggle(300);
var touchMove = function(e){
e.preventDefault();
};
if($(this).hasClass('is-active')){
$(this).removeClass('is-active');
window.removeEventListener( 'touchmove' , touchMove);
} else {
$(this).addClass('is-active');
window.addEventListener( 'touchmove' , touchMove , { passive: false } );
}
});
body 和 html
的 css 部分
html.toggle_bg, body.toggle_bg{
overflow:hidden;
}
整个html部分header
<div class="main_header">
<div class="header_parent_wrap" style="opacity: 1;">
<header>
<div class="logo_sect" data-height="85" style="height: 85px;">
<a class="logo" href="http://www.ryo-o-jpn.com/"><!-- Logo -->
<img alt="" height="85" src="http://www.ryo-o-jpn.com/wp-content/uploads/2019/08/ryookada-2.png" width="105"></a>
</div>
<div class="fright">
<nav class="menu-menu-container">
<ul class="menu" id="menu-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-302 current_page_item menu-item-305 parent-menu-1" id="menu-item-305">
<a href="http://www.ryo-o-jpn.com/">Home</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-328 parent-menu-2" id="menu-item-328">
<a href="http://www.ryo-o-jpn.com/about/">ABOUT</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-388 parent-menu-3" id="menu-item-388">
<a href="http://www.ryo-o-jpn.com/work/">WORK</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-341 parent-menu-4" id="menu-item-341">
<a href="http://www.ryo-o-jpn.com/contact/">CONTACT</a>
</li>
</ul>
</nav><!-- top_search -->
<div class="top_search">
<form action="http://www.ryo-o-jpn.com/" id="search_form" method="get" name="search_form">
<input class="ct-search-input" name="s" placeholder="To search type and hit Enter" type="text" value=""> <input class="s_submit" type="submit" value="Search"> <span class="top-icon-search"></span>
</form>
</div><!-- //top_search -->
<div class="clear"></div>
</div>
<div class="clear"></div>
</header>
<div class="mobile-navigation-toggle">
<div class="toggle-box">
<div class="toggle-inner"></div>
</div>
</div>
</div>
</div>
这是因为您尝试删除的事件侦听器与您添加的事件侦听器不同——它们是完全不同的对象。 (这是一个比 this question 稍微微妙的情况,因为您确实定义了一个变量来保存函数)。
问题是 touchMove
处理程序是在单击处理程序中定义的,因此每次单击 .mobile-navigation-toggle
时都会重新定义(作为不同的对象)。将它移到处理程序之外并且它可以工作 - 现在它是相同的,您添加和删除的全局处理程序。
var touchMove = function(e) {
console.log('touchMove handler active!');
e.preventDefault();
};
jQuery('.mobile-navigation-toggle').on("click", function() {
$('html').toggleClass('toggle_bg');
$('body').toggleClass('toggle_bg');
jQuery('.mobile_menu_wrapper').slideToggle(300);
if ($(this).hasClass('is-active')) {
$(this).removeClass('is-active');
console.log('removing handler');
window.removeEventListener('touchmove', touchMove);
} else {
$(this).addClass('is-active');
console.log('adding handler');
window.addEventListener('touchmove', touchMove, {
passive: false
});
}
});
html.toggle_bg,
body.toggle_bg {
overflow: hidden;
}
.mobile-navigation-toggle {
background-color: lightgrey;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main_header">
<div class="header_parent_wrap" style="opacity: 1;">
<header>
<div class="logo_sect" data-height="85" style="height: 85px;">
<a class="logo" href="http://www.ryo-o-jpn.com/">
<!-- Logo -->
<img alt="" height="85" src="http://www.ryo-o-jpn.com/wp-content/uploads/2019/08/ryookada-2.png" width="105"></a>
</div>
<div class="fright">
<nav class="menu-menu-container">
<ul class="menu" id="menu-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-302 current_page_item menu-item-305 parent-menu-1" id="menu-item-305">
<a href="http://www.ryo-o-jpn.com/">Home</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-328 parent-menu-2" id="menu-item-328">
<a href="http://www.ryo-o-jpn.com/about/">ABOUT</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-388 parent-menu-3" id="menu-item-388">
<a href="http://www.ryo-o-jpn.com/work/">WORK</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-341 parent-menu-4" id="menu-item-341">
<a href="http://www.ryo-o-jpn.com/contact/">CONTACT</a>
</li>
</ul>
</nav>
<!-- top_search -->
<div class="top_search">
<form action="http://www.ryo-o-jpn.com/" id="search_form" method="get" name="search_form">
<input class="ct-search-input" name="s" placeholder="To search type and hit Enter" type="text" value=""> <input class="s_submit" type="submit" value="Search"> <span class="top-icon-search"></span>
</form>
</div>
<!-- //top_search -->
<div class="clear"></div>
</div>
<div class="clear"></div>
</header>
<div class="mobile-navigation-toggle">
<div class="toggle-box">
<div class="toggle-inner">Click here to toggle touchMove handler</div>
</div>
</div>
</div>
</div>
我有这个 jquery 代码,我试图在用户单击汉堡包图标并且导航菜单一直下降时禁用滚动,并在他们关闭菜单时再次启用滚动.问题是,在移动版本中,我可以禁用滚动,但我无法再次启用它。即使在您关闭菜单后,window 也不会滚动。
这是 jquery 代码。
jQuery('.mobile-navigation-toggle').on("click", function() {
$('html').toggleClass('toggle_bg');
$('body').toggleClass('toggle_bg');
jQuery('.mobile_menu_wrapper').slideToggle(300);
var touchMove = function(e){
e.preventDefault();
};
if($(this).hasClass('is-active')){
$(this).removeClass('is-active');
window.removeEventListener( 'touchmove' , touchMove);
} else {
$(this).addClass('is-active');
window.addEventListener( 'touchmove' , touchMove , { passive: false } );
}
});
body 和 html
的 css 部分html.toggle_bg, body.toggle_bg{
overflow:hidden;
}
整个html部分header
<div class="main_header">
<div class="header_parent_wrap" style="opacity: 1;">
<header>
<div class="logo_sect" data-height="85" style="height: 85px;">
<a class="logo" href="http://www.ryo-o-jpn.com/"><!-- Logo -->
<img alt="" height="85" src="http://www.ryo-o-jpn.com/wp-content/uploads/2019/08/ryookada-2.png" width="105"></a>
</div>
<div class="fright">
<nav class="menu-menu-container">
<ul class="menu" id="menu-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-302 current_page_item menu-item-305 parent-menu-1" id="menu-item-305">
<a href="http://www.ryo-o-jpn.com/">Home</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-328 parent-menu-2" id="menu-item-328">
<a href="http://www.ryo-o-jpn.com/about/">ABOUT</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-388 parent-menu-3" id="menu-item-388">
<a href="http://www.ryo-o-jpn.com/work/">WORK</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-341 parent-menu-4" id="menu-item-341">
<a href="http://www.ryo-o-jpn.com/contact/">CONTACT</a>
</li>
</ul>
</nav><!-- top_search -->
<div class="top_search">
<form action="http://www.ryo-o-jpn.com/" id="search_form" method="get" name="search_form">
<input class="ct-search-input" name="s" placeholder="To search type and hit Enter" type="text" value=""> <input class="s_submit" type="submit" value="Search"> <span class="top-icon-search"></span>
</form>
</div><!-- //top_search -->
<div class="clear"></div>
</div>
<div class="clear"></div>
</header>
<div class="mobile-navigation-toggle">
<div class="toggle-box">
<div class="toggle-inner"></div>
</div>
</div>
</div>
</div>
这是因为您尝试删除的事件侦听器与您添加的事件侦听器不同——它们是完全不同的对象。 (这是一个比 this question 稍微微妙的情况,因为您确实定义了一个变量来保存函数)。
问题是 touchMove
处理程序是在单击处理程序中定义的,因此每次单击 .mobile-navigation-toggle
时都会重新定义(作为不同的对象)。将它移到处理程序之外并且它可以工作 - 现在它是相同的,您添加和删除的全局处理程序。
var touchMove = function(e) {
console.log('touchMove handler active!');
e.preventDefault();
};
jQuery('.mobile-navigation-toggle').on("click", function() {
$('html').toggleClass('toggle_bg');
$('body').toggleClass('toggle_bg');
jQuery('.mobile_menu_wrapper').slideToggle(300);
if ($(this).hasClass('is-active')) {
$(this).removeClass('is-active');
console.log('removing handler');
window.removeEventListener('touchmove', touchMove);
} else {
$(this).addClass('is-active');
console.log('adding handler');
window.addEventListener('touchmove', touchMove, {
passive: false
});
}
});
html.toggle_bg,
body.toggle_bg {
overflow: hidden;
}
.mobile-navigation-toggle {
background-color: lightgrey;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main_header">
<div class="header_parent_wrap" style="opacity: 1;">
<header>
<div class="logo_sect" data-height="85" style="height: 85px;">
<a class="logo" href="http://www.ryo-o-jpn.com/">
<!-- Logo -->
<img alt="" height="85" src="http://www.ryo-o-jpn.com/wp-content/uploads/2019/08/ryookada-2.png" width="105"></a>
</div>
<div class="fright">
<nav class="menu-menu-container">
<ul class="menu" id="menu-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-302 current_page_item menu-item-305 parent-menu-1" id="menu-item-305">
<a href="http://www.ryo-o-jpn.com/">Home</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-328 parent-menu-2" id="menu-item-328">
<a href="http://www.ryo-o-jpn.com/about/">ABOUT</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-388 parent-menu-3" id="menu-item-388">
<a href="http://www.ryo-o-jpn.com/work/">WORK</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-341 parent-menu-4" id="menu-item-341">
<a href="http://www.ryo-o-jpn.com/contact/">CONTACT</a>
</li>
</ul>
</nav>
<!-- top_search -->
<div class="top_search">
<form action="http://www.ryo-o-jpn.com/" id="search_form" method="get" name="search_form">
<input class="ct-search-input" name="s" placeholder="To search type and hit Enter" type="text" value=""> <input class="s_submit" type="submit" value="Search"> <span class="top-icon-search"></span>
</form>
</div>
<!-- //top_search -->
<div class="clear"></div>
</div>
<div class="clear"></div>
</header>
<div class="mobile-navigation-toggle">
<div class="toggle-box">
<div class="toggle-inner">Click here to toggle touchMove handler</div>
</div>
</div>
</div>
</div>