粘滞滚动容器在IE中不起作用

时间:2011-12-30 01:56:53

标签: javascript jquery internet-explorer internet-explorer-8

出于某种原因,这适用于所有主流浏览器,但不适用于IE(惊喜,惊喜)。

$(function() {
var stickyDiv = $('#js-sticky-wrap'); // div to clone
var clonedCss = 'js-sticky-fix'; // css class with fixed positioning
stickyContainer(stickyDiv, clonedCss);

function stickyContainer(stickyDiv, clonedCss, clonedWidth) {
    if (clonedWidth === undefined) { // use original container width
        var clonedWidth = $(stickyDiv).width();
    }

    var menuTop = stickyDiv.offset().top; // Capture visible content height
    var menuClone = stickyDiv.clone(true).addClass(clonedCss); // Clone current div, apply fixed style
    var dropShadow = $('<div class="js-drop-shadow"></div>').hide();
    // Append dropshadow style
    stickyDiv.append(dropShadow);

    $(window).bind('scroll', function() {
        var scrollY = window.pageYOffset; // get total px from vertical scroll
        if (scrollY > menuTop) { // they scrolled > than the original offset
            if (menuClone.parent().length === 0) {
               stickyDiv.addClass(clonedCss).width(clonedWidth);
                menuClone.after(stickyDiv.parent());
                dropShadow.show();
            }
        } else {
            stickyDiv.removeClass(clonedCss);

            dropShadow.hide();
            menuClone.remove();
        }

    });
}

});

Example on jsfiddle

我使用的是IE不支持的方法吗?似乎无法确定它,因为我没有在开发人员工具中收到任何错误。

1 个答案:

答案 0 :(得分:2)

在版本9之前,IE不支持

window.pageYOffset 。尝试使用它:

var scrollY;
if (typeof(window.pageYOffset) == 'number') {
    scrollY = window.pageYOffset;
}
else {
    scrollY = document.documentElement.scrollTop;
}