vue中touchEnd事件和touchStart、touchMove获取坐标不一样,IOS绑定touch事件失效

touchEnd    ~~~~~~~~~~~~~~~~   e.changedTouches[0].pageY

touchStart    ~~~~~~~~~~~~~~~~   e.targetTouches[0].pageY

touchMove    ~~~~~~~~~~~~~~~~   e.changedTouches[0].pageY

 1 activated() {
 2     this.listenerFunction()
 3   },
 4 destroyed() {
 5     window.removeEventListener('scroll', this.listenerFunction)
 6   },
 7 methods{
 8     scrollBottom() {
 9       const windowHeight = window.screen.height
10       const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
11       const contentHeight = this.$refs.scoreMallContainer.clientHeight
12       const toBottom = contentHeight - windowHeight - scrollTop
13       this.toBottom = toBottom
14     },
15     touchstart(e) {
16       this.startPageY = e.targetTouches[0].pageY
17     },
18     touchend(e) {
19       this.endPageY = e.changedTouches[0].pageY
20       if (!this.toBottom && this.endPageY - this.startPageY < 0) {
21         if (!this.$refs.taskView.popup) {
22           this.$refs.taskView.popupSwich()
23         }
24       }
25     },
26 }

 

1  popupSwich() {
2       this.popup = !this.popup
3       const taskBoxStyle = this.$refs.taskBox.style
4       this.popup ? taskBoxStyle.overflow = 'scroll' : taskBoxStyle.overflow = 'hidden'
5     },

 

不止是iOS,chrome等浏览器均存在类似问题,原因在于其在实现时的一点小改动: 浏览器只有在执行事件处理函数后才能知道有没有触发preventDefault,故页面会有延迟。为了解决这种延迟,从而让页面滚动的效果如丝般顺滑,从 chrome56 开始,在 window、document 和 body 上注册的 touchstart 和 touchmove 事件处理函数,会默认为是 passive: true。浏览器忽略 preventDefault() 就可以第一时间滚动了。
但这样的后果是,如果在以上这 3 个元素的 touchstart 和 touchmove 事件处理函数中调用 e.preventDefault() ,会被浏览器忽略掉,并不会阻止默认行为。

解决办法一:css touch-action: none;
解决办法二:注册处理函数时,用如下方式,明确声明为不是被动的 window.addEventListener('touchmove', func, { passive: false })

posted @ 2021-01-12 11:40  xfcao  阅读(3286)  评论(0编辑  收藏  举报