jQuery:当div变得可见时如何为其绑定事件?

发布于 2024-09-26 05:43:39 字数 290 浏览 8 评论 0原文

我有一个 div 元素:

标签数据

当此 div 变得可见时(获取 display: block;),如何绑定自定义事件?

而且我想在这个 div 变得不可见时绑定一个事件(获取 display: none;)。

我想用 jQuery 来做这件事。

编辑: 我正在使用 ajax 内容制作简单的选项卡。我希望仅当该选项卡可见时才通过 ajax 更新该选项卡上的内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

聽兲甴掵 2024-10-03 05:43:39

根据可见性启动和停止 AJAX 更新。您可以使用 .is() 来返回 TRUE 或 FALSE :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 示例

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

国际总奸 2024-10-03 05:43:39

让事件始终绑定到 div,但在事件内部,执行如下操作:

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

现在,仅当元素对用户可见时才会触发事件。

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.

香橙ぽ 2024-10-03 05:43:39

我找到的解决方案是在选择选项卡时启动 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
        }
    });
});

然后我捕获这些 focusblur 事件:

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

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
        }
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文