目前,我创建的移动导航遇到了一些问题。这是一个简单的汉堡包图标,当你点击它时,它会打开一个全屏幕菜单。问题是,当覆盖可见时,我试图禁用滚动。现在我想,我可以通过增加;
$('body').bind('touchmove', function(e){e.preventDefault()});这只工作一次,但当您再次关闭菜单时,preventDefault仍然处于活动状态,我不知道如何解除绑定,因为汉堡包图标用于打开和关闭菜单。
我添加了完整的js脚本,我在下面使用;
$(document).ready(function () {
$(".icon").click(function () {
$('body').bind('touchmove', function(e){e.preventDefault()});
$(".mobilenav").fadeToggle(500);
$(".top-menu").toggleClass("top-animate");
$(".mid-menu").toggleClass("mid-animate");
$(".bottom-menu").toggleClass("bottom-animate");
});
});谢谢你的帮助!
发布于 2017-08-03 11:51:38
使用.on()和.off() jQuery方法很容易实现!
$(document).on('touchmove', 'body', function(e){e.preventDefault()});
$(document).off('touchmove', 'body', function(e){e.preventDefault()});但是也有一个unbind()函数!
$('body').unbind('touchmove', function(e){e.preventDefault()});代码示例:
$(document).ready(function () {
var cancelScroll = function(e) { e.preventDefault(); }
$(".icon").click(function () {
if ($(".mobilenav").is(":visible")) {
$('body').unbind('touchmove', cancelScroll);
} else {
$('body').bind('touchmove', cancelScroll);
}
$(".mobilenav").fadeToggle(500);
$(".top-menu").toggleClass("top-animate");
$(".mid-menu").toggleClass("mid-animate");
$(".bottom-menu").toggleClass("bottom-animate");
});
});注意:从处理程序返回false等同于调用事件对象上的.preventDefault()和.stopPropagation()。
所以它可以是:
var cancelScroll = function() { return false; }发布于 2017-08-03 11:52:05
添加一个标志以区分和解除关闭时的绑定,
$(document).ready(function () {
var isClose = false;
$(".icon").click(function () {
if(isClose){
$('body').unbind('touchmove', function(e){e.preventDefault()});
isClose = false;
}else{
$('body').bind('touchmove', function(e){ e.preventDefault();});
isClose = true;
}
$(".mobilenav").fadeToggle(500);
$(".top-menu").toggleClass("top-animate");
$(".mid-menu").toggleClass("mid-animate");
$(".bottom-menu").toggleClass("bottom-animate");
});
});https://stackoverflow.com/questions/45483276
复制相似问题