Home > Article > Web Front-end > JavaScript implements disabling the pull-down rebound effect of WeChat browser
This article mainly introduces in detail the method of JavaScript to prohibit the pull-down rebound effect of WeChat browser. It has certain reference and value for learning JavaScript. Friends who are interested in JavaScript can refer to it. Take a look at this article
The example in this article shares with you the effect of JavaScript prohibiting pull-down rebound on WeChat browser for your reference. The specific content is as follows
Method 1:
<script type="text/javascript"> var overscroll = function(el){ el.addEventListener('touchstart', function(){ var top = el.scrollTop; var totalScroll = el.scrollHeight; var currentScroll = top + el.offsetHeight; if(top === 0) { el.scrollTop = 1; }else if(currentScroll === totalScroll){ el.scrollTop = top - 1; } }); el.addEventListener('touchmove', function(evt){ if(el.offsetHeight < el.scrollHeight){ evt._isScroller = true; } }); } overscroll(document.querySelector('.scroll'));//哪里需要可以局部滚动,添加一个“scroll”的class document.body.addEventListener('touchmove', function(evt) { if(!evt._isScroller){ evt.preventDefault(); } }); </script>
The advantages and disadvantages of this method:
Advantages: Support local scrolling;
Disadvantages: The browser itself The scrolling that appears beyond the page is disabled and needs to be changed to partial scrolling, and the "scroll" class needs to be added to the local scrolling place.
Note: If there are multiple partial scrolls on the same page, you need to change
overscroll(document.querySelector('.scroll');
to
for(var i=0;i<document.querySelectorAll('.scroll').length;i++){ overscroll(document.querySelectorAll('.scroll')[i]); }
Method 2:
<script type="text/javascript"> function stopDrop(){ var lastY;//最后一次y坐标点 $(document.body).on('touchstart', function(event) { lastY = event.originalEvent.changedTouches[0].clientY;//点击屏幕时记录最后一次Y度坐标。 }); $(document.body).on('touchmove', function(event) { var y = event.originalEvent.changedTouches[0].clientY; var st = $(this).scrollTop(); //滚动条高度 if (y >= lastY && st <= 10) {//如果滚动条高度小于0,可以理解为到顶了,且是下拉情况下,阻止touchmove事件。 lastY = y; event.preventDefault(); } lastY = y; }); } </script>
Advantages and disadvantages of this method:
Advantages : Supports scrolling of the browser itself beyond the page
Disadvantages : Does not support local scrolling
The above is the detailed content of JavaScript implements disabling the pull-down rebound effect of WeChat browser. For more information, please follow other related articles on the PHP Chinese website!