JSFiddle - React, Tailwind, and code Playground
HTML
<div id="foo"></div>
<div id="notification"></div>
CSS
html, body {
margin: 100px 0;
}
#foo {
height: 1000px;
background-color: #C00;
width: 200px;
margin: 0 auto;
}
#notification {
position: fixed;
top: 0;
left: 0;
}
}
JavaScript
$.fn.visibleHeight = function() {
var $window = $(window);
var elBottom, elTop, scrollBot, scrollTop, visibleBottom, visibleTop;
$(this).css('height', '');
scrollTop = $window.scrollTop();
scrollBot = scrollTop + $window.height();
elTop = this.offset().top;
elBottom = elTop + this.outerHeight();
visibleTop = elTop < scrollTop ? scrollTop : elTop;
visibleBottom = elBottom > scrollBot ? scrollBot : elBottom;
return visibleBottom - visibleTop;
}
$(window).on('scroll resize', function(){
console.log($("#foo").visibleHeight())
});