<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>test</title> </head> <body> <div id="box"></div> <script type="text/javascript"> function addData() { let box = document.getElementById('box'); for (let i = 0; i < 50; i++) { let span = document.createElement('span'); span.innerHTML = 'test' + i + '<br>'; box.appendChild(span); } } addData(); window.onscroll = function () { //scrollTop就是觸發滾輪事件時滾輪的高度 var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; console.log("滾動距離" + scrollTop); //變量windowHeight是可視區的高度 var windowHeight = document.documentElement.clientHeight || document.body.clientHeight; console.log("可視高度" + windowHeight); //變量scrollHeight是滾動條的總高度 var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight; console.log("總高度" + scrollHeight); //判斷滾動條是否到底部 if (scrollTop + windowHeight == scrollHeight) { //加載數據 console.log("距頂部" + scrollTop + "可視區高度" + windowHeight + "滾動條總高度" + scrollHeight); addData(); } } </script> </body> </html>