Calculate Offset Top Of Elements Inside Of A Scrollable Div
How can I calculate offset top inside a scrollable div? I have two divs that I want scroll inside my content div, and I want to set 2 variables to true/false, depending on where th
Solution 1:
I think this should get you most of the way there:
// Position of first element relative to container topvar scrollTop = $(".container .element").offset().top - $(".container").offset().top;
// Position of selected element relative to container topvar targetTop = $(".container > *").offset().top - $(".container").offset().top;
// The offset of a scrollable containervar scrollOffset = scrollTop - targetTop;
// Scroll untill target element is at the top of its container
$(".container").scrollTop(scrollOffset);
———————————
EDIT (May 31 2018)
I guess this is better:
var scrollOffset = $(".container .element")[0].offsetTop - $(".container")[0].offsetTop
Post a Comment for "Calculate Offset Top Of Elements Inside Of A Scrollable Div"