Menu Element.scrollHeight
 

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.

Top

The MDN Explaination of Element.scrollHeight:

The Element.scrollHeight read-only property is a measurement of the height of an element's content, including content not visible on the screen due to overflow.

The scrollHeight value is equal to the minimum height the element would require in order to fit all the content in the viewport without using a vertical scrollbar. The height is measured in the same way as clientHeight: it includes the element's padding, but not its border, margin or horizontal scrollbar (if present). It can also include the height of pseudo-elements such as ::before or ::after. If the element's content can fit without a need for vertical scrollbar, its scrollHeight is equal to clientHeight

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 scrollTop property of the Element interface gets or sets the number of pixels by which an element's content is scrolled from its top edge. This value is subpixel precise in modern browsers, meaning that it isn't necessarily a whole number.

The example on the MDN page is worth reproducing here.

Links

Top

References: - (didabled)

  • 1 - Element.scrollHeight - https:// developer.mozilla.org/en-US/docs/Web/API/Element/ scrollHeight
  • 2 - EventTarget.addEventListener() - https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
  • 3 - Element: scrollTop property - ttps:// developer.mozilla.org/en-US/ docs/Web/API/Element/scrollTop">