In my application, I have a button that adds field shapes to the body every time the button is clicked. I wanted to scroll to the new item, but this doesnβt work for obvious reasons (bubbles, etc.) ..). I came up with a simple solution ...
function scroll(pixels){ $('html, body').animate({ scrollTop: pixels }, 1000); }; function addObservation(x){ $('body').append(x); newFieldSet = $('fieldset').last(); pixelsFromTop = newFieldSet.offset().top; scroll(pixelsFromTop); }; $(document).on("click", "#add_observation", function(){ addObservation("<fieldset>SOME FORM FIELDS HERE</fieldset>"); });
That way, every time you add a set of fields, jQuery finds the last one added, then .offset().top measures how many pixels the set of fields has on top, and then scrolls the window at which the distance is.
skwidbreth Aug 09 '16 at 19:29 2016-08-09 19:29
source share