How to check if an element is in the view of the user with jquery

前端 未结 6 751
孤街浪徒
孤街浪徒 2020-11-30 04:28

I have a very big draggable div in my window. This div has a smaller window.

6条回答
  •  心在旅途
    2020-11-30 05:04

    https://github.com/sakabako/scrollMonitor

    var scrollMonitor = require("./scrollMonitor"); // if you're not using require, you can use the scrollMonitor global.
    var myElement = document.getElementById("itemToWatch");
    
    var elementWatcher = scrollMonitor.create( myElement );
    
    elementWatcher.enterViewport(function() {
        console.log( 'I have entered the viewport' );
    });
    elementWatcher.exitViewport(function() {
        console.log( 'I have left the viewport' );
    });
    
    elementWatcher.isInViewport - true if any part of the element is visible, false if not.
    elementWatcher.isFullyInViewport - true if the entire element is visible [1].
    elementWatcher.isAboveViewport - true if any part of the element is above the viewport.
    elementWatcher.isBelowViewport - true if any part of the element is below the viewport.
    

提交回复
热议问题