在到达特定Id时,在滚动时隐藏div元素

时间:2016-08-22 19:23:13

标签: javascript html

我有一个修复div,当我向下滚动并在向上滚动时显示它时,我需要在它达到特定ID时隐藏它。

1 个答案:

答案 0 :(得分:1)

试试这个:

$.fn.isOnScreen = function () {
    var win = $(window);
    var viewport = {
        top: win.scrollTop(),
        left: win.scrollLeft()
    };
    viewport.right = viewport.left + win.width();
    viewport.bottom = viewport.top + win.height();
    var bounds = this.offset();
    bounds.right = bounds.left + this.outerWidth();
    bounds.bottom = bounds.top + this.outerHeight();
    return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
};

并像这样使用它:

$(window).scroll(function () {
    if ($('#sampleId').isOnScreen() == true) {
        $('.fixDiv').hide();
    }
    else{
        $('.fixDiv').show();
    }
});