If I understand correctly, from your description it sounds like you are looking to see when the element enters the viewport.
That means you need to calculate the position of the bottom of the window (so that you can compare it to the position of the top of the element). You can calculate the position of the bottom of the window like so:
var windowBottom = $(this).scrollTop() + $(this).height();
Then you need to know at what position the element enters the viewport. You can get the top position of the element like so:
var elementTop = $(".element").offset().top;
You can then calculate how much of the element has been scrolled into the viewport by subtracting the element's top position from the position of the bottom of the window. You divide this number by the element's height to get the percentage like so:
var percentage = (windowBottom - elementTop) / $(".element").height() * 100;
Try the snippet below or check out this CodePen Demo:
$(document).ready(function() {
$(window).scroll(function() {
var windowBottom = $(this).scrollTop() + $(this).height();
var elementTop = $(".element").offset().top;
var percentage = (windowBottom - elementTop) / $(".element").height() * 100;
if (percentage >= 100) {
$(".percent").text('100%');
} else if (windowBottom >= elementTop) {
$(".percent").text(`${Math.round(percentage)}%`);
} else {
$(".percent").text('0%');
}
});
});
body {
margin: 0;
padding: 0;
color: white;
}
.flex-layout {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.info-container {
position: fixed;
z-index: 1;
font-size: 2em;
height: 100%;
width: 100%;
color: white;
}
.block {
height: 800px;
background: #333;
}
.element {
align-items: center;
background: #000;
height: 550px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='info-container flex-layout'>
<span>Scroll down</span>
<span class='percent'>0%</span>
</div>
<div class='block'></div>
<div class='element flex-layout'>
<span>Start</span>
<span>End</span>
</div>
<div class='block'></div>
I hope this helps and is what you're looking for.