Created
January 28, 2020 18:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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