原生js监听div id为main的height变化 并传递给function 不要jquery!

JavaScript013

原生js监听div id为main的height变化 并传递给function 不要jquery!,第1张

window.onload=function(){

    var mh=document.getElementById("main").offsetHeight

    setInterval(function(){

        var mh1=document.getElementById("main").offsetHeight

        if(mh1!=mh){

            console.log("main的height变化了!")

            mh=mh1

        }

    },100)

}

利用scroll 事件来监听。

当用户滚动指定的元素时,会发生 scroll 事件。

scroll 事件适用于所有可滚动的元素和 window 对象(浏览器窗口)。

scroll() 方法触发 scroll 事件,或规定当发生 scroll 事件时运行的函数。