How can I find what's the distance between top of the parent div and top of the child div ?
For example how can I know at what distance (height) div 2 starts related to parent div ? so I can scroll to it ?
How can I find what's the distance between top of the parent div and top of the child div ?
For example how can I know at what distance (height) div 2 starts related to parent div ? so I can scroll to it ?
You can use jQuery to find div2's position:
$('.div2').offset();
And how much you've already scrolled (if any):
var scrolled = $(window).scrollTop();
Then just subtract them
var distanceFromTop = offset.top - scrolled
This gives you the distance between the top of Div2, and the top of the viewport. Now you can tell your script to scroll down by that amount, or not to scroll if you're already past it.
Will this help in any way?
function pxToInt(val) {
return parseInt(val.replace('px', ''));
}
var a = pxToInt($('#parent').attr('padding-top'));
var b = pxToInt($('#div1').attr('margin-top'));
var c = pxToInt($('#div1').innerHeight());
alert(a + b + c);