Run Jquery function on window events: load, resize, and scroll?

后端 未结 3 968
孤城傲影
孤城傲影 2020-12-07 16:05

How do I run a jquery function on window events: load, resize, and scroll?

Here is my code I\'m trying to detect if a div is viewable and then if it is run some ajax

3条回答
  •  广开言路
    2020-12-07 16:42

    just call your function inside the events.

    load:

    $(document).ready(function(){  // or  $(window).load(function(){
        topInViewport($(mydivname));
    });
    

    resize:

    $(window).resize(function () {
        topInViewport($(mydivname));
    });
    

    scroll:

    $(window).scroll(function () {
        topInViewport($(mydivname));
    });
    

    or bind all event in one function

    $(window).on("load scroll resize",function(e){
    

提交回复
热议问题