几周以来,我一直在尝试解决以下代码,但无法弄清楚出了什么问题。当图标被选中时,下面的菜单会从左侧滑入和滑出,它还会像 Facebook 应用程序一样将元素移到右侧。但是,我需要它根据浏览器的大小略有不同(页面上的不同元素需要移动)。它在准备好文档时工作正常,但是当我调整浏览器大小时,它会尝试多次滑入和滑出,并且没有根据大小执行正确的滑动功能。任何人都可以建议吗?
var menuInitialized = false;
function doMenu() {
$(".c_left, .top_right, .c_right, .c_myaccount, .header_image, .c_footer, .copyright, .accepts, .myaccount, .header_logo").removeAttr('style');
var $menu = $(".c_left");
var width = $(window).width();
var status = 'closed';
if (width < 550) {
if (!menuInitialized) {
$('.icon-menu-2').on('click', function(event) {
alert('small'); //test which is being activated onclick
if (status === 'closed') {
$menu.animate({
width: 185,
marginLeft: 0,
display: 'toggle'
}, 'fast');
$(".top_right, .c_right, .c_myaccount, .c_footer, .copyright, .accepts").animate({
marginLeft: 185,
display: 'toggle'
}, 'fast');
$(".myaccount").animate({
marginRight: -185,
display: 'toggle'
}, 'fast');
return status = 'open';
} else if (status === 'open') {
$menu.animate({
width: 0,
marginLeft: -185,
display: 'toggle'
}, 'fast');
$(".top_right, .c_right, .c_myaccount,.c_footer, .copyright, .accepts").animate({
marginLeft: 0,
display: 'toggle'
}, 'fast');
$(".myaccount").animate({
marginRight: 0,
display: 'toggle'
}, 'fast');
return status = 'closed';
}
});
menuInitialized = true;
}
} else if ((width < 800) && (width > 550)) {
if (menuInitialized) {
$('.icon-menu-2').on('click', function(event) {
alert('large'); //test which is being activated onclick
if (status === 'closed') {
$menu.animate({
width: 185,
marginLeft: 0,
display: 'toggle'
}, 'fast');
$(".top_right, .c_right, .c_myaccount, .header_image, .c_footer, .copyright, .accepts").animate({
marginLeft: 185,
display: 'toggle'
}, 'fast');
$(".myaccount, .header_logo").animate({
marginRight: -185,
display: 'toggle'
}, 'fast');
return status = 'open';
} else if (status === 'open') {
$menu.animate({
width: 0,
marginLeft: -185,
display: 'toggle'
}, 'fast');
$(".top_right, .c_right, .c_myaccount, .header_image,.c_footer, .copyright, .accepts").animate({
marginLeft: 0,
display: 'toggle'
}, 'fast');
$(".myaccount, .header_logo").animate({
marginRight: 0,
display: 'toggle'
}, 'fast');
return status = 'closed';
}
});
menuInitialized = false;
}
}
}
$(document).ready(doMenu);
$(window).resize(doMenu);