Element.scrollHeight
The Javascript element.scrollHeight property along with the scrollTop property are very useful when you want to control user input to a webpage where thw screen is scrolled.
I have used the scrollTop property for the setting of the banner at the top of some of my webpages. In general the behaviour of the page when it is scrolled was one of the motivators for this page. A search for functions that can be used for this purpose led me to the example on the MDN website.
The example on this page, taken from a demo on MDN,shows the use of the Element.scrollHeight property to enable a check that a block of text has been scrolled to the end.
The example has a textarea element containig text that could be part of an agreement or licence terms. There is also a checkbox element and button control that are diabled until the text in the box is scrolled to the end.
The MDN Explaination of Element.scrollHeight:
The MDN Example - The license agreement
The code
The "notice" box and the "rules" are styled as below:
<style>
#notice {
display: inline-block;
margin-bottom: 12px;
border-radius: 5px;
width: 600px;
padding: 5px;
border: 2px #7FDF55 solid;
}
#rules {
width: 600px;
height: 130px;
padding: 5px;
border: #2A9F00 solid 2px;
border-radius: 5px;
}
</style>
The following function is called when the page is loaded:
script>
function checkReading () {
if (checkReading.read) {
return;
}
checkReading.read = this.scrollHeight - this.scrollTop === this.clientHeight;
document.registration.accept.disabled = document.getElementById("nextstep").disabled = !checkReading.read;
checkReading.noticeBox.innerHTML = checkReading.read ? "Thank you." : "Please, scroll and read the following text.";
}
onload = function () {
var oToBeRead = document.getElementById("rules");
checkReading.noticeBox = document.createElement("span");
document.registration.accept.checked = false;
checkReading.noticeBox.id = "notice";
oToBeRead.parentNode.insertBefore(checkReading.noticeBox, oToBeRead);
oToBeRead.parentNode.insertBefore(document.createElement("br"), oToBeRead);
oToBeRead.onscroll = checkReading;
checkReading.call(oToBeRead);
}
/script>
Enhancements
To confirm that a user has actually read the agreement, as opposed to just scrolling to the end, text is added that has to be entered for process to continue.
scrollTop
The operation of the example described on this page and the "agreement" page
The example on the MDN page is worth reproducing here.