You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
解决这个问题可以用下面代码,指定的 DIV 容器可以内部滚动,这里就让 react 组件渲染的 DOM 节点 去支持滚动为例。
letoverscroll=function(el){el.addEventListener('touchstart',function(){lettop=el.scrollToplettotalScroll=el.scrollHeightletcurrentScroll=top+el.offsetHeight//If we're at the top or the bottom of the containers//scroll, push up or down one pixel.////this prevents the scroll from "passing through" to//the body.if(top===0){el.scrollTop=1}elseif(currentScroll===totalScroll){el.scrollTop=top-1}})el.addEventListener('touchmove',function(evt){//if the content is actually scrollable, i.e. the content is long enough//that scrolling can occurif(el.offsetHeight<el.scrollHeight)evt._isScroller=true})}overscroll(document.querySelector('#app'))document.body.addEventListener('touchmove',function(evt){console.log(evt._isScroller)//In this case, the default behavior is scrolling the body, which//would result in an overflow. Since we don't want that, we preventDefault.if(!evt._isScroller){evt.preventDefault()}},{passive: false})
正常情况下,微信浏览器页面下拉后可以看到一个 『该网页由 xxxx 提供』,因为页面会提供给第三方使用,因此有一个需求是隐藏该网址。
因为页面都是已经存在的,并且有很多个页面,因此想要的解决方案是一个通用的解决方案(至少也得90%页面通用),不去修改具体每个页面的代码。
零、补充一点
可以使用 CSS 来实现这个需求
线上案例
一、思路
解决这个问题,直接 google 一顿搜索,得出下面的结论。
1.1、看微信开发者文档
首先需要查一下微信开发者的文档,看下是JS SDK 有方法可以用来控制这个网址的显示隐藏,遗憾的是,并没有提供这类方法。
1.2、禁止 touchmove 滚动
既然『该网页由 xxxx 提供』是页面已经滑到顶部了继续下拉才出现的,那么禁止页面下拉不就可以解决问题了嘛,小菜一碟。
too young too simple ,
addEventListener
方法的第三个参数有兼容性问题。现在这个时间点,chrome 78 已经出来了, 因此禁止页面滚动就成了这样。
这个时候,你会发现,页面的确不能下拉,那么 『该网页由 xxxx 提供』 不就看不到了吗,完美。
1.3、body 不滚动,单需要让 div 容器内部滚动
不过改出新问题了,页面不能滚动了,超过一屏的页面,只能看一半,这谁能忍。因此还需要解决 禁止 touchmove 后,页面滚动的问题。
解决这个问题可以用下面代码,指定的 DIV 容器可以内部滚动,这里就让 react 组件渲染的 DOM 节点 去支持滚动为例。
只有这个 js 还是有问题的,需要设置一下 #app 的高度,并且超出长度则滚动。
因为div 容器内的滚动效果不太好,没有弹性滚动,就是手指停止滑动,页面就停止滑动了。因为增加了
-webkit-overflow-scrolling: touch;
来加一个弹性滚动效果。1.4、注意 -webkit-overflow-scrolling: touch 的 Bug
解决这些问题,可以参考这个文章,文章写的还是比较详细了的,给作者点个赞。 《深入研究-webkit-overflow-scrolling:touch及ios滚动》。
Bug 的表现就是:偶尔卡住或不能滑动。
解决方案:就是在
webkit-overflow-scrolling:touch
属性的下一层子元素上,将height加1%或1px。从而主动触发scrollbar。当然偶尔卡住了只是其中一个问题, 除此之外,这个属性还有很多bug,包括且不限于以下几种:
1、滚动中 scrollTop 属性不会变化
2、手势可穿过其他元素触发元素滚动
3、滚动时暂停其他 transition
二、总结一下
PC Web 的时候,开发最苦逼的是 IE 兼容性,现在 Mobile Web 开发,最苦逼的各个手机的兼容性问题。兼容性是前端开发避不开的深坑呀。
解决一个问题,又引发了更多的问题出来,生生不息,无穷尽也。
这篇文章的内容,就大概下面这些了。
终、参考文章
1、《微信浏览器禁止页面下拉查看网址(不影响页面内部scroll)》
2、《深入研究-webkit-overflow-scrolling:touch及ios滚动》
3、《[移动端新特性] Passive Event Listeners》
4、《passive 的事件监听器》
5、《移动端禁用及恢复触摸页面滚动》
The text was updated successfully, but these errors were encountered: