鍍金池/ 問答/HTML5  HTML/ 移動端 input 輸入框獲取焦點(diǎn)后無法立即滾動到可視區(qū)域

移動端 input 輸入框獲取焦點(diǎn)后無法立即滾動到可視區(qū)域

input 輸入框獲取焦點(diǎn)后無法立即滾動到可視區(qū)域,只有輸入內(nèi)容后才會滾動到可視區(qū)域.想要獲取焦點(diǎn)后立即滾動到可視區(qū)域應(yīng)該怎么實(shí)現(xiàn)?
31546620-5b081132-b024-11e7-851f-51c124bae16f.gif

回答
編輯回答
心癌

解決方案如下:

window.addEventListener('resize', () => {
    const activeElement = document.activeElement
    if (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA') {
        setTimeout(() => {
          activeElement.scrollIntoView()
        }, 100)
      }
    })
2017年5月1日 01:01