jQuery: How to bind an event for the div when it becomes visible?

前端 未结 3 2038
予麋鹿
予麋鹿 2021-01-18 15:28

I\'ve got a div element:

Tab data
.

How to bind a custom event when this div becomes visible (gets displa

3条回答
  •  礼貌的吻别
    2021-01-18 16:05

    The solution I found was to fire up focus event when the tab is selected.

    var tabContainers = $('div.tabs > div');
    
    $('div.tabs ul.tabNavigation a').click(function () {
        tabContainers.each(function(){
    
            tabContainers.hide().filter(this.hash).show();
    
            if ( $(this).is(':visible') ) {
                $(this).focus(); // fire this event if tab is visible
            } else {
                $(this).blur(); // if tab is invisible
            }
        });
    });
    

    And then I catch these focus and blur events:

    $(document).ready(function(){
        $("#tabID").bind("focus",function(){
            if ( $(this).is(":visible") ) {
                // start ajax here
            }
        });
    
        $("#tab'.$key.'").bind("blur",function(){
            if ( !$(this).is(":visible") ) {
                // stop ajax here
            }
        });
    });
    

提交回复
热议问题