Skip to content

Instantly share code, notes, and snippets.

@StefanoChiodino
Created February 6, 2019 15:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanoChiodino/46c55d0cb166d9c69845b3bffa9b18f0 to your computer and use it in GitHub Desktop.
Save StefanoChiodino/46c55d0cb166d9c69845b3bffa9b18f0 to your computer and use it in GitHub Desktop.
$(document).ready(function () {
function scrollToFormIfInteractedWith() {
// Find a target to scroll to if the user is interacting with forms. This happens in different ways:
// - Find a submit message that is not wrapped in a form (the message is always present even when not visible
// as the form has just been loaded.
// - If the message wasn't found then try to target a form containing a hidden field that indicates that the form's
// step/page is not the first(0) one.
// - If that wasn't found either then try to find a form that has PreviousClicked = true where the user just
// navigated back. This is CURRENTLY NOT WORKING possibly because of an Umbraco Forms issue for which I've raise
// a github issue here https://github.com/umbraco/Umbraco-CMS/issues/4443.
let target = $('.umbraco-forms-submitmessage:not(form .umbraco-forms-submitmessage)')[0]
|| $('input[type="hidden"][name="FormStep"]:not([value="0"]),'
+ 'input[type="hidden"][name="PreviousClicked"][value="true"]').parent('form')[0];
// If we have found a target to scroll to then smooth scroll there.
if (target) {
// If inside an accordion then click it to expand it.
$(target).parent('.accordion-panel').children('button.accordion-panel__button').click();
// Scroll page to the target. Here 20 is just to add some margin, but you could need to add
// the height of your sticky navbar too.
let targetOffset = $(target).offset().top - 20;
$('html,body').animate({scrollTop: targetOffset});
}
}
scrollToFormIfInteractedWith();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment