欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-04 20:50:28
微信浏览器禁止页面下拉查看网址实例详解 此类事件是手机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/index.html

源码下载:

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