views:

87

answers:

3

I've got a div element: <div id="tab1"> Tab data </div>.

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

And also I'd like to bind an event when this div becomes invisible (gets display: none;).

I'd like to do this in jQuery.

Edit: I'm making simple tabs with ajax content. I want the content on this tabs to be ajax-updated only when the tab is visible.

+1  A: 

Have the event always bound to the div, but inside the event, do something like the following:

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

Now your event will only be triggered if the element is visible to the user.

Mike Trpcic
+1  A: 

Start and stop the AJAX update based on the visibility. You can use .is() to return a TRUE or FALSE for :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);
}

Here is a simple example of a timer that stops when it is invisible. Note that the numbers don't keep increasing when they're not visible:

(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 example


Of course, in the above case, and maybe your case, it's more straight forward to simple alternately turn the update on and off:

(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 example

Peter Ajtai
This jsFiddle service blew my mind! It's awesome. Thanks.
SaltLake
My question was not about how to start/stop sending ajax requests, but about how to bind an event to div when it becomes visible (when the tab is active).
SaltLake
@SaltLake - Yeah. jsFiddle is pretty useful and fun!
Peter Ajtai
A: 

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