当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信浏览器禁止页面下拉查看网址实例详解

微信浏览器禁止页面下拉查看网址实例详解

2017年12月12日  | 移动技术网IT编程  | 我要评论
微信浏览器禁止页面下拉查看网址实例详解 此类事件是手机touchmove默认事件行为,可以通过js代码隐藏事件: $(‘body').on(‘touchmove

微信浏览器禁止页面下拉查看网址实例详解

此类事件是手机touchmove默认事件行为,可以通过js代码隐藏事件:

$(‘body').on(‘touchmove', function (event) {event.preventdefault();});
or
document.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();
 }
});

详情见:https://github.com/luster-io/prevent-overscroll/blob/master/

源码下载:

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网