jQuery event to trigger action when a div is made visible

后端 未结 22 2209
你的背包
你的背包 2020-11-22 12:03

I\'m using jQuery in my site and I would like to trigger certain actions when a certain div is made visible.

Is it possible to attach some sort of \"isvisible\" even

相关标签:
22条回答
  • 2020-11-22 12:16

    If you want to trigger the event on all elements (and child elements) that are actually made visible, by $.show, toggle, toggleClass, addClass, or removeClass:

    $.each(["show", "toggle", "toggleClass", "addClass", "removeClass"], function(){
        var _oldFn = $.fn[this];
        $.fn[this] = function(){
            var hidden = this.find(":hidden").add(this.filter(":hidden"));
            var result = _oldFn.apply(this, arguments);
            hidden.filter(":visible").each(function(){
                $(this).triggerHandler("show"); //No bubbling
            });
            return result;
        }
    });
    

    And now your element:

    $("#myLazyUl").bind("show", function(){
        alert(this);
    });
    

    You could add overrides to additional jQuery functions by adding them to the array at the top (like "attr")

    0 讨论(0)
  • 2020-11-22 12:19

    Use jQuery Waypoints :

    $('#contentDiv').waypoint(function() {
       alert('do something');
    });
    

    Other examples on the site of jQuery Waypoints.

    0 讨论(0)
  • 2020-11-22 12:20

    a hide/show event trigger based on Glenns ideea: removed toggle because it fires show/hide and we don't want 2fires for one event

    $(function(){
        $.each(["show","hide", "toggleClass", "addClass", "removeClass"], function(){
            var _oldFn = $.fn[this];
            $.fn[this] = function(){
                var hidden = this.find(":hidden").add(this.filter(":hidden"));
                var visible = this.find(":visible").add(this.filter(":visible"));
                var result = _oldFn.apply(this, arguments);
                hidden.filter(":visible").each(function(){
                    $(this).triggerHandler("show");
                });
                visible.filter(":hidden").each(function(){
                    $(this).triggerHandler("hide");
                });
                return result;
            }
        });
    });
    
    0 讨论(0)
  • 2020-11-22 12:20

    What helped me here is recent ResizeObserver spec polyfill:

    const divEl = $('#section60');
    
    const ro = new ResizeObserver(() => {
        if (divEl.is(':visible')) {
            console.log("it's visible now!");
        }
    });
    ro.observe(divEl[0]);
    

    Note that it's crossbrowser and performant (no polling).

    0 讨论(0)
  • 2020-11-22 12:20

    Just bind a trigger with the selector and put the code into the trigger event:

    jQuery(function() {
      jQuery("#contentDiv:hidden").show().trigger('show');
    
      jQuery('#contentDiv').on('show', function() {
        console.log('#contentDiv is now visible');
        // your code here
      });
    });
    
    0 讨论(0)
  • 2020-11-22 12:22

    redsquare's solution is the right answer.

    But as an IN-THEORY solution you can write a function which is selecting the elements classed by .visibilityCheck (not all visible elements) and check their visibility property value; if true then do something.

    Afterward, the function should be performed periodically using the setInterval() function. You can stop the timer using the clearInterval() upon successful call-out.

    Here's an example:

    function foo() {
        $('.visibilityCheck').each(function() {
            if ($(this).is(':visible')){
                // do something
            }
        });
    }
    
    window.setInterval(foo, 100);
    

    You can also perform some performance improvements on it, however, the solution is basically absurd to be used in action. So...

    0 讨论(0)
提交回复
热议问题