将jQuery滚动代码转换为纯javascript

时间:2016-07-13 18:20:49

标签: javascript jquery scroll code-conversion

我非常了解jQuery但是使用纯JavaScript我有问题。

你能帮我转换jquery代码到javascript吗?谢谢:))

这是我的代码。

var container = $('.container');

$(window).scroll(function(event) {
    var scrollTop = $(window).scrollTop();
    var containerTop = container.position().top;
    var containerHeight = container.outerHeight();
    var blockHeight = $('.game-block').height();
    var callPosition = (containerTop + containerHeight) - blockHeight;

    if(scrollTop >= callPosition) {
            //do ajax call
    }
});

1 个答案:

答案 0 :(得分:-1)

var container = document.getElementByClassName('container');

window.addEventListener('scroll', function() {
    var scrollTop = window.scrollY;
    var containerTop = container.offsetTop;
    var containerHeight = container.offsetHeight;
    var blockHeight = document.getElementByClassName('game-block').clientHeight;
    var callPosition = (containerTop + containerHeight) - blockHeight;
});