微信浏览器禁止页面下拉查看网址(不影响页面内部scroll)
此类事件是手机touchmove默认事件行为,可以通过js代码隐藏事件:
$(‘body’).on(‘touchmove’, function (event) {event.preventDefault();}); or document.addEventListener('touchmove', function(e){e.preventDefault()}, false); or document.body.addEventListener('touchmove', function(e) {e.preventDefault();},false);
但这样往往会把页面原生的scroll效果也一同去掉了,下面的代码可以完美解决这个问题:
var overscroll = function(el) { el.addEventListener('touchstart', function() { var top = el.scrollTop , totalScroll = el.scrollHeight , currentScroll = 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; } else if(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 occur if(el.offsetHeight < el.scrollHeight) evt._isScroller = true; }); } overscroll(document.querySelector('.scroll')); document.body.addEventListener('touchmove', function(evt) { //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(); } }); 作者:我是7号_frank 链接:https://www.jianshu.com/p/2eddee561971 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
jetamiett 2018-04-12
郴州小程序 2020-06-13
xiaonao00 2020-05-27
且听真言 2020-05-15
znbvx 2020-03-09
xiaonao00 2020-03-03
cbao 2019-12-21
xiaonao00 2019-12-11
颤抖吧腿子 2019-12-05
liangliangshuo 2019-11-03
vqudominiapp 2019-09-08
82387864 2018-01-22
wsjb00 2019-06-05
tumobi 2019-05-17
zzzerttt 2019-04-05
quiyo 2018-08-16
zonehh 2019-07-01
目尽地平线 2019-07-01