I created a DEMO below:
$(document).ready(function() {
var container_height = $("#container").height();
var first_position = container_height - $("#first").position().top;
var second_position = container_height - $("#second").position().top;
var last_position = container_height - $("#last").position().top;
console.log(`First position: ${first_position}`)
console.log(`Second position: ${second_position}`)
console.log(`Last position: ${last_position}`)
})
#first, #second, #last {
width: 100px;
height: 100px;
padding: 30px;
margin: 3px;
text-align: center;
}
#first {
background: #556622;
}
#second {
background: #773399;
}
#last {
background: #996677;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="first">FIRST</div>
<div id="second">SECOND</div>
<div id="last">LAST</div>
</div>
You can calculate the distance from the end of your container to the element by calculating: container's height - the top position of your element.
$(document).ready(function() {
var container_height = $("#container").height();
var first_position = container_height - $("#first").position().top;
var second_position = container_height - $("#second").position().top;
var last_position = container_height - $("#last").position().top;
console.log(first_position, second_position, last_position);
});
Example of html:
<div id="container">
<div id="first">FIRST</div>
<div id="second">SECOND</div>
<div id="last">LAST</div>
</div>
Some css for nice styles:
#first, #second, #last {
width: 100px;
height: 100px;
padding: 30px;
margin: 3px;
text-align: center;
}
#first {
background: #556622;
}
#second {
background: #773399;
}
#last {
background: #996677;
}
So, when this distance is less than 100px (let's say) you will change from your A to B case.