Skip to content

Instantly share code, notes, and snippets.

@zakiya
Created January 28, 2020 18:46
Show Gist options
  • Save zakiya/eace5f4b9256dc9e04c7cd7cf09a7902 to your computer and use it in GitHub Desktop.
Save zakiya/eace5f4b9256dc9e04c7cd7cf09a7902 to your computer and use it in GitHub Desktop.
Scroll to position of submit button so users can see the new content that's displayed below.
(function($, Drupal) {
"use strict";
Drupal.behaviors.submitscroll = {
attach: function(context) {
// Define button.
const $submitButton = $(".jcc-choice-section input[type=submit]");
// Send offset values to sessionStorage, then submit form.
$submitButton.click(function(e) {
sessionStorage.windowOffset = window.pageYOffset;
sessionStorage.submitPosition = $submitButton.offset().top;
$("form.cc-user-input").submit();
});
// Check sessionStorage for value.
if (sessionStorage.windowOffset > 1) {
var $newPosition = 0;
if (
// Submit button is near the bottom of the window.
sessionStorage.submitPosition - window.innerHeight >
sessionStorage.windowOffset - 200
) {
$newPosition = parseInt(sessionStorage.windowOffset) + 200;
} else {
// Submit button is not near the bottom of the window.
$newPosition = sessionStorage.windowOffset;
}
// Scroll based on offset values before submit.
$("html, body").animate({ scrollTop: $newPosition }, 200);
}
}
};
})(jQuery, Drupal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment