I am trying to get new data when reaching the end of the content of a div element with overflow: scroll
I have searched for an example of this but to with no success. I have researched on MDN and found the wheel event, in the event I found the LayerY property
The method I thought might work is as follows. When scrolling down and if the value of the LayerY
property is repeated n
times, it is at the end of the content, so I make the call to the resource.
For example
let repeat = 0;
let value = 0;
const div = document.querySelector('div');
div.addEventListener('wheel', wheelHandler);
function wheelHandler(event) {
if (value > 150 && value === event.layerY) {
repeat += 1;
}
if (value > 150 && repeat > 3) {
console.log('fetch data');
repeat = 0; // set repeat to 0 after fetch data
}
value = event.layerY;
}
* {
box-sizing: border-box;
}
div {
width: 500px;
height: 200px;
border: 1px solid gray;
overflow: scroll;
padding: 10px;
}
<div>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Sed exercitationem
doloribus cum, atque quibusdam nemo dolorem, quia omnis, libero consequatur
molestiae error facilis maxime pariatur delectus aliquam sunt porro vel.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Sed exercitationem
doloribus cum, atque quibusdam nemo dolorem, quia omnis, libero consequatur
molestiae error facilis maxime pariatur delectus aliquam sunt porro vel.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Sed exercitationem
doloribus cum, atque quibusdam nemo dolorem, quia omnis, libero consequatur
molestiae error facilis maxime pariatur delectus aliquam sunt porro vel.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Sed exercitationem
doloribus cum, atque quibusdam nemo dolorem, quia omnis, libero consequatur
molestiae error facilis maxime pariatur delectus aliquam sunt porro vel.
</div>
There are other scenarios, for example handle if you scroll up
Is there a native way to achieve the expected result? either is this path correct
I appreciate your advice and ideas