Event listener for when element becomes visible?

后端 未结 9 2026
感情败类
感情败类 2020-11-28 05:36

I am building a toolbar that is going to be included into a page. the div it is going to be included in will default to display:none. Is there a way i can p

9条回答
  •  时光取名叫无心
    2020-11-28 06:16

    my solution:

    ; (function ($) {
    $.each([ "toggle", "show", "hide" ], function( i, name ) {
        var cssFn = $.fn[ name ];
        $.fn[ name ] = function( speed, easing, callback ) {
            if(speed == null || typeof speed === "boolean"){
                var ret=cssFn.apply( this, arguments )
                $.fn.triggerVisibleEvent.apply(this,arguments)
                return ret
            }else{
                var that=this
                var new_callback=function(){
                    callback.call(this)
                    $.fn.triggerVisibleEvent.apply(that,arguments)
                }
                var ret=this.animate( genFx( name, true ), speed, easing, new_callback )
                return ret
            }
        };
    });
    
    $.fn.triggerVisibleEvent=function(){
        this.each(function(){
            if($(this).is(':visible')){
                $(this).trigger('visible')
                $(this).find('[data-trigger-visible-event]').triggerVisibleEvent()
            }
        })
    }
    })(jQuery);
    

    for example:

    if(!$info_center.is(':visible')){
        $info_center.attr('data-trigger-visible-event','true').one('visible',processMoreLessButton)
    }else{
        processMoreLessButton()
    }
    
    function processMoreLessButton(){
    //some logic
    }
    

提交回复
热议问题