获取可滚动div中的当前视图

时间:2018-04-18 08:03:08

标签: javascript jquery html css

我正在制作一张有很多照片的照片库。我想使用延迟加载,只加载屏幕中可见的图像。画廊是一个div,因为整个画廊是一个更大的项目的一部分。

我想要什么:获取div的当前滚动位置,以便检查图像是否在div中可见。

我有什么:我在全窗口滚动条上工作,我只需将其更改为div。

        var pItem = document.getElementsByClassName('progressive replace'), timer;

function inView() {
        var wT = window.pageYOffset, wB = wT + window.innerHeight, cRect, pT, pB, p = 0;
        while (p < pItem.length) {

            cRect = pItem[p].getBoundingClientRect();
            pT = wT + cRect.top;
            pB = pT + cRect.height;

            if (wT < pB && wB > pT) {
                loadFullImage(pItem[p]);
                pItem[p].classList.remove('replace');
            }
            else p++;
        }
    }

我需要这样的东西:

function inView() {
        var wT = $('.image_view').pageYOffset, wB = wT + $('.image_view').innerHeight, cRect, pT, pB, p = 0;
        while (p < pItem.length) {

            cRect = pItem[p].getBoundingClientRect();
            pT = wT + cRect.top;
            pB = pT + cRect.height;

            if (wT < pB && wB > pT) {
                loadFullImage(pItem[p]);
                pItem[p].classList.remove('replace');
            }
            else p++;
        }
    }

image_view是可滚动的div,里面有图像。

1 个答案:

答案 0 :(得分:0)

我使用jQuery和scrolltop得到了当前的滚动值;

$('#scrollableDiv').scrollTop();
相关问题