J'ai la fonction ci-dessous qui fonctionne comme prévu sur document prêt, mais sur le redimensionnement, il ne pas enregistrer le changement de taille et exécute l'alerte pour la taille avant le redimensionnement. J'ai ajouté le menuInitialized
comme indicateur pour essayer de combattre le problème mais maintenant il s'initialise plusieurs fois. Par exemple, il alertera "Small", "Large", "small" Si je redimensionne le navigateur plusieurs fois.Redimensionner le navigateur JQuery initier la fonction
var menuInitialized = false;
function doMenu() {
var left = $('.c_left').height();
var right = $('.c_right').height();
if (left > right) {
$('.c_right').css('height', left + 'px');
}
$(".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';
var width = $(window).width();
if (width < 550) {
if (!menuInitialized) {
$('.icon-menu-2').on('click', function(event) {
alert('small');
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');
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);
Où la variable 'width' vient? Vous devez mettre 'width' à jour lors du redimensionnement! – adeneo
En outre, il se déclenchera une seule fois, car la condition if échouera sur tout sauf la première fois que l'utilisateur redimensionne. – adeneo
Est-ce que 'menuInitialized' est à nouveau défini sur' false'? À l'heure actuelle, le code à l'intérieur de ce bloc 'if (! MenuInitialized)' ne peut s'exécuter qu'une seule fois, car '! MenuInitialized' cesse d'être vrai une fois entré dans le bloc. – apsillers