我有一个修复div,当我向下滚动并在向上滚动时显示它时,我需要在它达到特定ID时隐藏它。
答案 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();
}
});