Vue custom instruction-click the element input input box on the mobile terminal to vertically center the page after the soft keyboard pops up

Vue.directive('gomiddle', {
    // 当被绑定的元素插入到 DOM 中时……
    inserted: function(el) {
        el.addEventListener('click', function() {
            setTimeout(function() {
                let top = el.offsetTop;
                let windowScrollTop = document.documentElement.scrollTop;
                let windowHeight = window.innerHeight;
                document.documentElement.scrollTop = windowScrollTop + ((top - windowScrollTop) - (windowHeight / 2))
            }, 1000)
        }, false)
    }
})

 

Guess you like

Origin blog.csdn.net/liuhao9999/article/details/115366098