class pageChannelExPlan {
    constructor() {
        this.loading = false
        this.finished = false

        this.$footer = $('.dw-common-footer')
        this.$listWrap = $('.video-list__wrap')
        this.$videoList = this.$listWrap.children('.video-list')
        this.$videoItems = this.$videoList.children('li')

        this.init()
    }

    init() {
        this.loadMore()
    }

    loadMore() {
        var self = this
        var footHeight = this.$footer.height()
        var screenHeight = $(window).height()
        $(window).scroll(function () {
            var $body = $('body')
            var $imgs = $('img[data-src]'),
                $win = $(this),
                offsetBottom = $win.scrollTop() + $win.height()//底部的offsetY
            $imgs.each((index, item) => {
                var $item = $(item)
                if (offsetBottom >= $item.offset().top - 100)
                    $item.attr('src', $item.attr('data-src')).removeAttr('data-src')
            })
            if ($(this).scrollTop() + screenHeight + footHeight >= $body.height() && !self.loading) {
                self.getData()
            }
        })
    }

    getData() {
        if (this.finished) return
        this.loading = true
        var self = this
        setTimeout(function () {
            var $list = self.$videoItems.filter(':hidden')

            //显示20条数据
            $list.each((index, ele) => {
                if (index >= 20) {
                    return;
                } else {
                    $(ele).show()
                }
            })

            if ($list.length - 20 <= 0) {
                self.finished = true
            }

            $(window).scroll();

            self.loading = false
        }, 100);
    }
}

$(() => {
    new pageChannelExPlan
})
