How to get/catch event off screen element?

我试图使固定的表头,当你向下滚动和表头关闭屏幕时,可以捕获事件或捕获元素?


One method would be using a scroll event for the window, and check if the element is visible there.

How to check if an element is on screen is answered here: Check if element is visible on screen

Here's how to turn that code into custom events entered_view and exited_view:

var $element = $('#some_element');
var is_visible = false;

$(window).on('scroll', function (e) {
    // Check if $element is on screen using code from other answer

    if (checkVisible($element)) {
         if (!is_visible) {
             $element.trigger('entered_view');
         }
         is_visible = true;
    } else {
         if (is_visible) {
             $element.trigger('exited_view');
         }
         is_visible = false;
    }
});

Once you get it working, you might also want to debounce the scroll event: Here's info about debouncing/throttling in jquery.

链接地址: http://www.djcxy.com/p/83604.html

上一篇: 如果HTML元素在屏幕上,如何不移动页面?

下一篇: 如何获取/捕获屏幕元素事件?