更新时间:2022-06-18 07:13:21
你可以计算出来使用 onscroll 事件滚动条当前位置的百分比,如果达到可以使用 scrollTo 将50%的滚动位置设置到页面顶部> function:
You can calculate the percentage of the current position of the scrollbar using the onscroll event, and if it reaches the 50 % the scroll position can be set to the top of the page with the scrollTo function:
window.onload = function () {
window.onscroll = function () {
var doc = document.body,
scrollPosition = doc.scrollTop,
pageSize = (doc.scrollHeight - doc.clientHeight),
percentageScrolled = Math.floor((scrollPosition / pageSize) * 100);
if (percentageScrolled >= 50){ // if the percentage is >= 50, scroll to top
window.scrollTo(0,0);
}
};
};
您可以查看我的示例这里。