完美解决移动端H5页面的滑动穿透问题

同事的分享,记录下来。

代码如下:

css:

body.modal-open {
  position: fixed;
  width: 100%;
}

js:

// 兼容低版本 document.scrollingElement写法
      (function () {
        if (document.scrollingElement) {
          return;
        }
        var element = null;
        function scrollingElement () {
          if (element) {
            return element;
          } else if (document.body.scrollTop) {
            // speed up if scrollTop > 0
            return (element = document.body);
          }
          var iframe = document.createElement('iframe');
          iframe.style.height = '1px';
          document.documentElement.appendChild(iframe);
          var doc = iframe.contentWindow.document;
          doc.write('<!DOCTYPE html><div style="height:9999em">x</div>');
          doc.close();
          var isCompliant = doc.documentElement.scrollHeight > doc.body.scrollHeight;
          iframe.parentNode.removeChild(iframe);
          return (element = isCompliant ? document.documentElement : document.body);
        }
        Object.defineProperty(document, 'scrollingElement', {
          get: scrollingElement
        });
      })();
      var ModalHelper = (function (bodyCls) {
        var scrollTop;
        return {
          afterOpen: function () {
            scrollTop = document.scrollingElement.scrollTop;
            document.body.classList.add(bodyCls);
            document.body.style.top = -scrollTop + 'px';
          },
          beforeClose: function () {
            document.body.classList.remove(bodyCls);
            // scrollTop lost after set position:fixed, restore it back.
            document.scrollingElement.scrollTop = scrollTop;
          }
        };
      })('modal-open');

然后在打开遮罩层的地方添加如下js:

ModalHelper.afterOpen();

在关闭遮罩层的地方添加如下js:

ModalHelper.beforeClose();

这样,你再也不用因为页面的滑动穿透而烦恼啦~

 

顺便再分享一些关于滚动的优化方法:

1.消除难看的滚动条:在父元素的css添加如下代码

scrollbar-width: none;
::-webkit-scrollbar {display:none}

2.让滚动显得更加流畅:在父元素的css添加如下代码

overflow-y: scroll;
/* 增加弹性滚动,解决滚动不流畅的问题 */
-webkit-overflow-scrolling: touch;

 3.补充:虽说穿透解决了,但是ios手机频繁滑动后会出现页面假死的bug,后面使用了以下代码优化了一下:

iosScrollFix: function (className) {
    var startY, startTopScroll;
    var ua = navigator.userAgent.toLowerCase();
    if (/iphone|ipad|ipod/.test(ua)) {
      var temp = document.querySelectorAll(className);
      var tempLen = temp.length;
      for (var i = 0; i < tempLen; ++i) {
        let
          j = i;
        temp[j].addEventListener('touchstart', function (event) {
          startY = event.touches[0].pageY;
          startTopScroll = temp[j].scrollTop;
        }, false);
        temp[i].addEventListener('touchmove', function (event) {
          var startY2 = event.touches[0].pageY;
          if (startTopScroll <= 0 && startY2 - startY > 0) {
            event.preventDefault();
          }
          if (startTopScroll + temp[j].offsetHeight >= temp[j].scrollHeight && startY2 - startY < 0) {
            event.preventDefault();
          }
        });
      }
    }
  }

 

转载于:https://www.cnblogs.com/xinsir/p/10310663.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在jQuery移动端H5页面开发中,我们可以使用以下技巧和方法: 1. 使用viewport设置移动端页面的宽度和缩放比例,例如: ```html <meta name="viewport" content="width=device-width, initial-scale=1.0"> ``` 2. 使用CSS3媒体查询来适配不同的屏幕尺寸,例如: ```css @media screen and (max-width: 768px) { /* 在屏幕宽度小于等于768px时应用的样式 */ } ``` 3. 使用jQuery Mobile框架来快速构建移动端页面,例如: ```html <!-- 引入jQuery Mobile库 --> <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"> <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> <!-- 使用jQuery Mobile组件 --> <div data-role="page"> <div data-role="header"> <h1>Header</h1> </div> <div data-role="content"> <p>Content</p> </div> <div data-role="footer"> <h4>Footer</h4> </div> </div> ``` 4. 使用touch事件来处理移动端的触摸事件,例如: ```javascript $(document).on("touchstart", function(event) { // 处理touchstart事件 }); $(document).on("touchmove", function(event) { // 处理touchmove事件 }); $(document).on("touchend", function(event) { // 处理touchend事件 }); ``` 5. 使用CSS3动画来实现移动端页面的动态效果,例如: ```css @keyframes myanimation { 0% { transform: translateX(0); } 50% { transform: translateX(100px); } 100% { transform: translateX(0); } } .myelement { animation: myanimation 1s ease-in-out infinite; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值