【问题标题】:Javascript Drawer SliderJavascript 抽屉滑块
【发布时间】:2017-06-09 01:18:53
【问题描述】:

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

$("#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");
    }
});

【问题讨论】:

  • 为什么你认为你在抽屉展开之后记录它?该代码指示您在动画开始时(而不是结束时)转储值。 Animate 以异步方式运行。

标签: javascript jquery html css frontend


【解决方案1】:

正如 Gone Coding 提到的:

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

【讨论】:

    猜你喜欢
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    相关资源
    最近更新 更多