私信  •  关注

Romain Linsolas

Romain Linsolas 最近创建的主题
Romain Linsolas 最近回复了
17 年前
回复了 Romain Linsolas 创建的主题 » 当内容位于可见视区时启动jquery动画

我的应用程序中有这样一个方法,但它不使用jquery:

/* Get the TOP position of a given element. */
function getPositionTop(element){
    var offset = 0;
    while(element) {
        offset += element["offsetTop"];
        element = element.offsetParent;
    }
    return offset;
}

/* Is a given element is visible or not? */
function isElementVisible(eltId) {
    var elt = document.getElementById(eltId);
    if (!elt) {
        // Element not found.
        return false;
    }
    // Get the top and bottom position of the given element.
    var posTop = getPositionTop(elt);
    var posBottom = posTop + elt.offsetHeight;
    // Get the top and bottom position of the *visible* part of the window.
    var visibleTop = document.body.scrollTop;
    var visibleBottom = visibleTop + document.documentElement.offsetHeight;
    return ((posBottom >= visibleTop) && (posTop <= visibleBottom));
}

编辑:这种方法对i.e.(至少版本6)很有效。请阅读注释以了解与FF的兼容性。