Find offset relative to parent scrolling div instead of window

You could just subtract the offset of the parent element from the offset of the child element. Will that complicate the other things you need to do as well?

$(".getoffset").offset().top - $(".getoffset").offsetParent().offset().top

This is probably what you are looking for

var scrollOffset = $('.container .element')[0].offsetTop - $('.container')[0].offsetTop;

and in Vanilla JS

var scrollOffset = document.querySelector('.container .element').offsetTop - document.querySelector('.container').offsetTop;


If you want background this should get you most of the way there:

// Position of first element relative to container top
var scrollTop = $(".container .element").offset().top - $(".container").offset().top;

// Position of selected element relative to container top
var targetTop = $(".container > *").offset().top - $(".container").offset().top;

// The offset of a scrollable container
var scrollOffset = scrollTop - targetTop;

// Scroll untill target element is at the top of its container

Also see: Calculate offset top of elements inside of a scrollable div