I am having parent with fixed height (400px) and child div height more than parent div height (400px - child div's top value) should be based on below conditions.
So, Finally Child div height should be 360px (400px - child div's top value of -40px)
HTML:
<div class="parent">
<div class="child">
Parent Height: <span id="parentHeight"></span><br>
Distance from top: <span id="distanceFromTop"></span><br>
Child Height: <span id="childHeight"></span><br>
</div>
</div>
Script:
var __parentHeight = $('.parent').height();
var __distanceFromTop = $('.parent').offset().top - $('.child').offset().top;
var __finalHeight = parseInt(__parentHeight) - parseInt(__distanceFromTop);
$('#parentHeight').html(__parentHeight + 'px');
$('#distanceFromTop').html(__distanceFromTop + 'px');
$('#childHeight').html(__finalHeight + 'px');
$('.child').css('height', __finalHeight + 'px');
Expected:
What I am getting is:
Update your final height calculation like that => var __finalHeight = parseInt(__parentHeight) - parseInt(__distanceFromTop);