I want to call focus()
on an input after the widow scrolled. I'm using the smooth behavior for the scrollTo()
method. The problem is the focus
method cut the smooth behavior. The solution is to call the focus
function just after the scroll end.
But I can't find any doc or threads speaking about how to detect the end of scrollTo
method.
let el = document.getElementById('input')
let elScrollOffset = el.getBoundingClientRect().top
let scrollOffset = window.pageYOffset || document.documentElement.scrollTop
let padding = 12
window.scrollTo({
top: elScrollOffset + scrollOffset - padding,
behavior: 'smooth'
})
// wait for the end of scrolling and then
el.focus()
Any ideas?
scrollTo()
but I think you can trysetTimeout()
only if you know the duration of scrolling! – JoyannscrollTo
method :/ – Wyne