如果在视口中将元素替换x秒

时间:2016-08-02 15:12:57

标签: javascript

Hello目前我正在使用此代码,检查元素是否在视口中,然后用其他东西替换它。任何人有任何想法如何才能让它替换元素只有在视口中,例如至少2秒?

var test = {
    elementViewportChecker: function(el) {
        var rect = el.getBoundingClientRect(),
            top = rect.top,
            height = rect.height,
            el = el.parentNode;

        do {
            rect = el.getBoundingClientRect();
            if(top <= rect.bottom === false)
                return false;

            if((top + height) <= rect.top)
                return false;

            el = el.parentNode;
        } while(el != document.body);

        return top <= document.documentElement.clientHeight;
    },
    isElementIntoView: function(el) {
        return this.elementViewportChecker(el);
    },
};

if(test.isElementIntoView(element)) {
   element.doSomething();
}

我尝试了多种方式,但没有一种方法可行。如果有人能帮助我,我真的很感激。

0 个答案:

没有答案