It seems to be an issue with Firefox. This workaround can fix it seemlessly.
Make sure you don't have the smooth-scrolling behavior enabled.
html {
/* Make sure this line is disabled! */
/* scroll-behavior: smooth; */
}
Create a variable which will be used to store your current Y scroll position later, might be a class attribute too.
let currentScroll = 0;
At the hCaptcha initialization, point it to a callback which will be fired when the challenge will appear.
function onCaptchaOpen() {
if (('netscape' in window) && / rv:/.test(navigator.userAgent)) {
window.scrollTo({ top: currentScroll });
}
}
let widgetId = window.hcaptcha.render('contact-captcha', {
sitekey: '10000000-ffff-ffff-ffff-000000000001',
size: 'invisible',
'open-callback': onCaptchaOpen,
});
Then find the submit button, and store the current scroll position right before triggering a captcha challenge.
function validate(event) {
event.preventDefault();
currentScroll = (window.pageYOffset || document.scrollTop) - (document.clientTop || 0);
window.hcaptcha
.execute(widgetId, { async: true })
.then(({ response }) => console.log(response));
}
const submitButton = $('#submit-button').get(0);
submitButton.onclick = validate;
Fixed codepen: https://codepen.io/aisouard/pen/NWYEbpp