safari 局部滑动时禁止body 滚动 -- 移动端 、禁止页面滑动 antd 、select、 threeSelect

开发中使用antd ui 开发移动端页面

下拉框展开时 滑动下拉框内容 body 也会被拉动
  • 猜测 原因是touchmove 事件穿透
  • 处理方式: JS 禁止事件穿透
    CSS 禁止BODY页面滚动

CSS 无法处理iOS微信端 情况 只能使用JS

组件获取焦点时设置 禁止滚动
  const body = useRef(document.getElementsByTagName('body'));
  // 聚焦时
  const unScoll = () => {
    if (/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)) {
       body.current[0].setAttribute('ontouchmove', 'event.preventDefault()');
    } else {
       body.current[0].style.overflowY = 'hidden';
    }
  };
  // 失焦时
  const scoll = () => {
    if (/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)) {
       body.current[0].setAttribute('ontouchmove', '');
    } else {
       body.current[0].style.overflowY = 'scroll';
    }
  };

微信打开网页 不兼容ontouchmove='event.preventDefault()

 // 判断是否微信打开
  const isWechartView = () => {
    const ua = navigator.userAgent.toLowerCase();
		if(ua.match(/MicroMessenger/i)?.toString() === "micromessenger") {
      return true
    } else {
      return false
		}
  }

JS 处理方案

  • 阻止默认事件
// 此事件必须提到组件外  不可简写在监听内部
const  event = (e: any) => {
  e.preventDefault();
}

// 禁止滑动调用
  const unScoll = () => {
    document.addEventListener('touchmove', event, { passive: false });
  };
 // 启动滑动
  const scoll = () => {
    document.removeEventListener('touchmove', event, false);
  };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值