为什么translateX不能像IE9,IE10和IE11上的固定元素一样工作?

Dec*_*ava 7 html css internet-explorer css-position

我试图在IE9,IE10和IE11中实现以下功能(在Chrome和FF上完美运行):

在移动模式下,我有一个主#container包装器,它包含整个站点内容和一个导航侧菜单div,它位于#container(不能移出,顺便说一句)里面,但是不可见并隐藏在屏幕外.当用户单击菜单打开切换按钮时,它应#container向右滑动,显示直接位于其左侧的导航侧菜单div.使用translateX进行"滑动",一旦"打开"类通过切换应用于它,就会被分配.在IE中,我按预期获得动画部分,但没有可见的侧面导航(仅限空白空间).

#container {
    height: 100%;
    position: relative;
    transition: transform ease .5s;
    width: 100%;
}
#container.open {
    position: fixed;
    transform: translateX(300px);
}

#nav-side-menu {
    left: -300px;
    height: 100%;
    overflow: scroll;
    position: fixed;
    top: 0;
    width: 300px;
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*son 14

这里的问题是position: fixed 变换元素使用.根据规范,当使用固定定位的元素时...包含块由视口建立. 关于转换元素是否应该是固定后代的包含块存在争议,但Internet Explorer目前不支持这一点.

在这个特定的实例中,您可以通过完全避免CSS转换来避免跨浏览器的复杂性.相反,尝试使用left属性横向移动包含元素.以下是我的标记 - 我相信这是你的合理反映:

<article>
    <nav>
        <p>This is the navigation portion.</p>
    </nav>
    <section>
        <p>This is the content portion.</p>
    </section>
</article>
Run Code Online (Sandbox Code Playgroud)

如上所述,以下方法使得相对定位的容器的关键使用通过转换(自IE10支持)left属性而左右移动.我们还使用该calc功能(自IE9起支持)来确定更好的尺寸和偏移:

body {
    margin: 0;
    overflow-x: hidden;
}

article {
    left: -300px;
    position: relative;
    transition: left 2s;
    box-sizing: border-box;
    width: calc(100% + 300px);
    padding: 0 1em 0 calc(300px + 1em);
}

article.open {
    left: 0px;
}

nav {
    position: fixed;
    width: 300px; height: 100%;
    margin: -1em auto auto calc(-300px - 1em);
}
Run Code Online (Sandbox Code Playgroud)

这种方法可以在Internet Explorer,Chrome和Firefox中提供更一致的体验.最终结果可以在这里在线查看:http://jsfiddle.net/jonathansampson/vxntq8b1/

在此输入图像描述

  • 这种方法的缺点是它对浏览器来说更有用.转换`left`属性将触发布局,绘画和合成.如果IE能够使用固定位置元素的变换,那么通过使用变换,您只需要更改合成,不需要重新绘制或重新布局.有关这些属性的性能成本的更多信息,请访问http://csstriggers.com/ (5认同)