在涉及的input中设置blur事件,事件里具体使用的方法如下
setTimeout(() => {
window.scrollTo(0, 0)
}, 100)
如果当前页面有多个
inputFocus (index) {
this.lastFocusInputIndex = index
},
inputBlur (index) {
let self = this
self.lastFocusInputIndex = index
setTimeout(() => {
self.checkInputBlur(index)
}, 100)
},
checkInputBlur (lastIndex) {
if (this.lastFocusInputIndex === lastIndex) {
window.scrollTo(0, 0)
this.lastFocusInputIndex = -1
}
}
网上看到的方法二
(function () {
let myFunction
let isWXAndIos = isWeiXinAndIos()
if (isWXAndIos) {
document.body.addEventListener('focusin', () => {
clearTimeout(myFunction)
})
document.body.addEventListener('focusout', () => {
clearTimeout(myFunction)
myFunction = setTimeout(function() {
window.scrollTo({top: 0, left: 0, behavior: 'smooth'})
}, 200)
})
}
})()
function isWeiXinAndIos () {
let ua = '' + window.navigator.userAgent.toLowerCase()
let isWeixin = /MicroMessenger/i.test(ua)
let isIos = /\(i[^;]+;( U;)? CPU.+Mac OS X/i.test(ua)
return isWeixin && isIos
}
最近使用的方法三
var u = navigator.userAgent
var sAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
// var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
// 安卓
if (isAndroid) {
window.addEventListener('resize', function() {
if (document.activeElement.tagName == "INPUT") {
window.setTimeout(function() {
document.activeElement.scrollIntoViewIfNeeded() // 异常时可以用 document.activeElement.scrollIntoView(false)
}, 0)
}
})
}
function inputFocus (type) {
console.log('focus type === ', type)
}
function inputBlur (type) {
console.log('blur type === ', type)
setTimeout(() => {
window.scrollTo(0, 0)
}, 100)
}