How to check if an element is off-screen

后端 未结 7 1809
太阳男子
太阳男子 2020-11-27 11:39

I need to check with jQuery if a DIV element is not falling off-screen. The elements are visible and displayed according CSS attributes, but they could be intentionally plac

7条回答
  •  無奈伤痛
    2020-11-27 12:03

    • Get the distance from the top of the given element
    • Add the height of the same given element. This will tell you the total number from the top of the screen to the end of the given element.
    • Then all you have to do is subtract that from total document height

      jQuery(function () {
          var documentHeight = jQuery(document).height();
          var element = jQuery('#you-element');
          var distanceFromBottom = documentHeight - (element.position().top + element.outerHeight(true));
          alert(distanceFromBottom)
      });
      

提交回复
热议问题