简体   繁体   English

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

[英]Why doesn't translateX work as expected for fixed elements on IE9, IE10, and IE11?

I'm trying to achieve the following in IE9, IE10, and IE11 (works perfectly on Chrome and FF): 我试图在IE9,IE10和IE11中实现以下功能(在Chrome和FF上完美运行):

In mobile mode, I have a main #container wrapper that holds the entire site contents and a nav side menu div which is inside the #container (cannot be moved out, btw), yet is not visible and is hidden off-screen. 在移动模式下,我有一个主#container包装器,它包含整个站点内容和一个导航侧菜单div,它位于#container (不能移出,顺便说一句),但是不可见,并且隐藏在屏幕外。 When a user clicks a menu open toggle button, it should slide the #container to the right, revealing the nav side menu div directly positioned to its left. 当用户单击菜单打开切换按钮时,它应将#container向右滑动,显示直接位于其左侧的导航侧菜单div。 The "sliding" is happening using translateX, which gets assigned as soon as the "open" class gets applied to it via the toggle. 使用translateX进行“滑动”,一旦“打开”类通过切换应用于它,就会被分配。 In the IEs, I'm getting the animation part as expected, but without a visible side nav (empty space only). 在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;
}

The problem here is with the use of position: fixed inside a transformed element. 这里的问题是使用position: fixed 一个转换元素中position: fixed Per the specification, when using fixed-positioned elements ...the containing block is established by the viewport. 根据规范,当使用固定定位的元素时...包含块由视口建立。 There is a debate as to whether transformed elements should be the containing block of fixed descendants, but Internet Explorer doesn't presently support this. 关于转换元素是否应该是固定后代的包含块存在争议 ,但Internet Explorer目前不支持这一点。

In this particular instance you could avoid the cross-browser complications by avoiding CSS Transforms altogether. 在这个特定的实例中,您可以通过完全避免CSS转换来避免跨浏览器的复杂性。 Instead, try moving the containing element laterally using the left property. 相反,尝试使用left属性横向移动包含元素。 Below is my markup — which I believe to be a reasonable reflection of yours: 以下是我的标记 - 我相信这是你的合理反映:

<article>
    <nav>
        <p>This is the navigation portion.</p>
    </nav>
    <section>
        <p>This is the content portion.</p>
    </section>
</article>

As described above, the following approach makes key use of a relatively positioned container, moved side-to-side by transitioning (supported since IE10) the left property. 如上所述,以下方法使得相对定位的容器的关键使用通过转换(从IE10支持) left属性而left We're also using the calc function (supported since IE9) to determine better dimensions and offsets: 我们还使用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);
}

This approach yields a more consistent experience across both Internet Explorer, Chrome, and Firefox. 这种方法可以在Internet Explorer,Chrome和Firefox中提供更一致的体验。 The end-result can be viewed online here: http://jsfiddle.net/jonathansampson/vxntq8b1/ 最终结果可以在这里在线查看: http//jsfiddle.net/jonathansampson/vxntq8b1/

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM