2017-01-24 139 views
0

试图检查位置固定元素的宽度。在点击菜单图标容器时,抽屉的宽度应从0%增加到18%。如果文件正文被点击,抽屉应该关闭,但只有当抽屉宽度为18%时。当我尝试将抽屉的宽度记录到控制台时,我得到0px,即使在抽屉展开后记录结果。有人知道我错过了什么吗?Javascript抽屉滑块

$("#menu-icon-container").click(function(){ 
    $("#drawer-menu-wrapper").animate({width:'18%'},'fast'); 
    $("#drawer-home-text").text("Home"); 
    $("#drawer-products-text").text("Products"); 
    $("#body-wrapper").css("opacity","0.5"); 
    console.log($("#drawer-menu-wrapper").css("width")); 
}); 
$("#close-icon-container").click(function(){ 
    $("#drawer-menu-wrapper").animate({width:'0%'},'fast'); 
    $("#drawer-home-text").text(""); 
    $("#drawer-products-text").text(""); 
    $("#body-wrapper").css("opacity","1"); 
}); 
$("#body-wrapper").click(function(){ 
    if ($("#drawer-menu-wrapper").css("width")!="0px"){ 
     $("#drawer-menu-wrapper").animate({width:'0%'},'fast'); 
     $("#drawer-home-text").text(""); 
     $("#drawer-products-text").text(""); 
     $("#body-wrapper").css("opacity","1"); 
    } 
}); 
+1

为什么你认为你正在登录它* *后的抽屉被扩展?该代码表示​​您在动画开始后立即转储该值(而不是结束时)。 Animate异步操作。 –

回答

0

如飘编码算法中提到:

$("#menu-icon-container").click(function(){ 
    $("#drawer-menu-wrapper").animate({width:'18%'},'fast',function(){ 
     console.log($("#drawer-menu-wrapper").css("width")); 
    }); 
});