jQuery event to trigger action when a div is made visible

后端 未结 22 2612
你的背包
你的背包 2020-11-22 12:03

I\'m using jQuery in my site and I would like to trigger certain actions when a certain div is made visible.

Is it possible to attach some sort of \"isvisible\" even

22条回答
  •  醉话见心
    2020-11-22 12:16

    If you want to trigger the event on all elements (and child elements) that are actually made visible, by $.show, toggle, toggleClass, addClass, or removeClass:

    $.each(["show", "toggle", "toggleClass", "addClass", "removeClass"], function(){
        var _oldFn = $.fn[this];
        $.fn[this] = function(){
            var hidden = this.find(":hidden").add(this.filter(":hidden"));
            var result = _oldFn.apply(this, arguments);
            hidden.filter(":visible").each(function(){
                $(this).triggerHandler("show"); //No bubbling
            });
            return result;
        }
    });
    

    And now your element:

    $("#myLazyUl").bind("show", function(){
        alert(this);
    });
    

    You could add overrides to additional jQuery functions by adding them to the array at the top (like "attr")

提交回复
热议问题