Tab data. How to bind a custom ...">

JQuery: how to bind an event for a div when it becomes visible?

I have an element div: <div id="tab1"> Tab data </div>.

How to bind a custom event when this div becomes visible (gets display: block;)?

And also I want to bind an event when this div becomes invisible (receives display: none;).

I would like to do this in jQuery.

Edit: I make simple tabs with ajax content. I want the content on these tabs to be updated by ajax only when the tab is visible.

+5
source share
3 answers

Start and stop the visibility based AJAX update. You can use to return TRUE or FALSE for : .is() :visible

var timer; // Variable to start and top updating timer

  // This if statement has to be part of the event handler for the visibility
  //   change of selector..... so there might be more straight forward solution
  //   see the last example in this answer.
if ($(selector).is(":visible"))
{
    // Start / continue AJAX updating
    timer = setInterval(AJAXupdate, 1000);
} else
{
    // Stop AJAX updating
    clearInterval(timer);
}

, , . : , :

(function() {    

    var switcher;                             // variable to start and stop timer

      // Function / event that will be started and stopped
    function count() {
        $("div").html(1 + parseInt($("div").html(), 10));
    }

    $(function() {                                               // <== doc ready

          // Start timer - it is visible by default
        switcher = setInterval(count, 1000);

        $("input").click(function() {

            $("div").toggle();                         // Toggle timer visibility

              // Start and stop timer based on visibility
            if ($("div").is(":visible"))
            {
                switcher = setInterval(count, 1000);
            } else
            {
                clearInterval(switcher);            
            }
        });
    });
}());

jsFiddle


, , , , , , :

(function() {    

    var switcher;

    function count() {
        $("div").html(1 + parseInt($("div").html(), 10));
    }

    $(function() {

        switcher = setInterval(count, 1000);

        $("input").toggle(function() { 
            clearInterval(switcher);
            $("div").toggle(); }, 
        function() {                        
            switcher = setInterval(count, 1000);
            $("div").toggle();
        });

    });

}());

jsFiddle

+3

div, , :

if($(self).is(":visible")){
    // Implement your code
}

, .

+3

, , , focus, .

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
        }
    });
});

focus blur:

$(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
        }
    });
});
+1

All Articles