English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Code Implementation of WeChat Page Scrolling Anti-Bottom Exposure (Core Code)

I found a piece of code online that can prevent the page from scrolling to the top or bottom when exposed to WeChat's ugly gray background. Below, the editor shares the core code with everyone for reference!

My core code:

prevent:function () {
var startX = 0, startY = 0;
//touchstart event
function touchSatrtFunc(evt) {
try
{
//evt.preventDefault(); //Prevent the browser from zooming, scrolling, etc. during touch
var touch = evt.touches[0]; //Get the first touch point
var x = Number(touch.pageX); //X coordinate of the page touch point
var y = Number(touch.pageY); //Y coordinate of the page touch point
//Record the initial position of the touch point
startX = x;
startY = y;
} catch (e) {
alert('touchSatrtFunc:'; + e.message);
}
}
document.addEventListener('touchstart', touchSatrtFunc, false);
var _ss = document.getElementById("contain");
_ss.ontouchmove = function (ev) {
var _point = ev.touches[0],
_top = _ss.scrollTop;
// When Will It Reach the Bottom
var _bottomFaVal = _ss.scrollHeight - _ss.offsetHeight;
// Reached the Top
if (_top === 0) {
// Prevent Downward Swiping
if (_point.clientY > startY) {
ev.preventDefault();
} else {
// Prevent Bubbling
// Normal Execution
ev.stopPropagation();
}
}
// Reached the Bottom
// Prevent Upward Swiping
if (_point.clientY < startY) {
ev.preventDefault();
} else {
// Prevent Bubbling
// Normal Execution
ev.stopPropagation();
}
} else if (_top > 0 && _top < _bottomFaVal) {
ev.stopPropagation();
} else {
ev.preventDefault();
}
};
}

The following code is the core code of the anti-exposure bottom core code of the WeChat page introduced by the editor. The code is relatively simple and is hoped to be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time!

Declaration: The content of this article is from the network, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, does not undergo artificial editing, and does not assume relevant legal responsibility. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.