• 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 })

  • 相关阅读:
    vue-cli3.0配置开发环境,测试环境,线上环境
    jQuery使用CDN加速
    浏览器中JavaScript脚本代码的load、ready等方法的加载顺序
    使用 JavaScript 拦截和跟踪浏览器中的 HTTP 请求
    Node和NPM在Windows环境下绿色安装及配置
    Nodejs 中将html转换成pdf文件
    数学励志公式:每天进步一点点
    网页调用打印机打印时纸张A4的设置
    用JS或jQuery访问页面内的iframe,兼容IE/FF
    HTML to DOM
  • 原文地址:https://www.cnblogs.com/xfcao/p/14266198.html
Copyright © 2020-2023  润新知