在创建网站(Queenalright.co.uk)时,我在IE中定为固定DIV遇到问题。当我试图创造一个响应的布局时,这是更困难的,因此需要流体所需的各种元素的宽度。我使用脚本调用了IE的目标版本,解决了这一点。从那时起,IE11已被释放和(在它的前进思维中)删除了条件评论,但未添加对居中固定或绝对div元素的支持!

解决问题的最佳方法是什么,或者是否有一种完整的方式来实现这一问题,我不知道?

这是脚本:

// script to achieve horizontal centering in internet exporer! Grrrrr!

function topSize() {


    // get screen width
    var screenWidth = $(window).width();

    // resize
    var halfScreenWidth = screenWidth/2;
    var topWidth = $('#top').width();
    var topBGWidth = $('#top-bg').width();
    var minusMarginInner = (-1)*(topWidth/2);
    var minusMarginBG = (-1)*(topBGWidth/2);


    $('#top').css({
        "left":"50%",
        "right":"auto",  // overwrites the right 1% property previously used for centering
        "margin-left":minusMarginInner + "px"
    });

    $('#top-bg').css({
        "left":"50%",
        "right":"0",  // overwrites the right 1% property previously used for centering
        "margin-left":minusMarginBG + "px"
    });

}


$(document).ready(function() {
var screenWidth = $(window).width();
if (screenWidth >= 1096) {
    topSize();
}
}); // document ready test window size and run topsize function

$(window).resize(function() {
var screenWidth = $(window).width();
if (screenWidth >= 1096) {
    topSize();
}
}); // window resize test window size and run topsize function
.

有帮助吗?

解决方案

如果发现自己询问如何单独瞄准IE,您可能会更好地休息并重新考虑您所采取的方法。Internet Explorer可以将绝对/固定元素置于任何其他浏览器。在这个特殊的情况下,你所采取的方法不是兼容的跨浏览器。我可以建议替代品。

.center {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
}
.

这种方法将在所有现代浏览器上都有相同的工作,为您提供一致的体验。我创建了一个小型演示小提琴演示了这一点: http://jsfiddle.net/rx2jb/show/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top