Table of Contents

DOM Event - Scroll

About

scroll is an view event that happens when the user scroll

If you want to disable scrolling, don't forget to disable the key up and down. See keydown block

Example

Pure Javascript

function isElementVisible(el) {
    const rect = el.getBoundingClientRect(),
        vWidth = window.innerWidth || document.documentElement.clientWidth,
        vHeight = window.innerHeight || document.documentElement.clientHeight,
        efp = function (x, y) {
            return document.elementFromPoint(x, y)
        };

    // Return false if it's not in the viewport
    if (rect.right < 0 || rect.bottom < 0
        || rect.left > vWidth || rect.top > vHeight)
        return false;

    // Return true if any of its four corners are visible
    return (
        el.contains(efp(rect.left,  rect.top))
        ||  el.contains(efp(rect.right, rect.top))
        ||  el.contains(efp(rect.right, rect.bottom))
        ||  el.contains(efp(rect.left,  rect.bottom))
    );
}
let lastKnownScrollPosition = 0;
let ticking = false;

const pSelected = document.querySelectorAll('p');

document.addEventListener('scroll', function(e) {

  if (!ticking) {
    window.requestAnimationFrame(function() {
      for (i = 0; i < pSelected.length; i++) {
             let p = pSelected[i];
             if (isElementVisible(p)) {
                  p.innerHTML = '<p>'+p.innerText+' - In view</p>';
              }
      }
      ticking = false;
    });

    ticking = true;
  }
});
<p>1</br>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>

Jquery

Extracted from the scroll jquery reference.

$( "p" ).clone().appendTo( document.body );
$( "p" ).clone().appendTo( document.body );
$( "p" ).clone().appendTo( document.body );
$( "p" ).clone().appendTo( document.body );
$( window ).scroll(function() {
  $( "span" ).css( "display", "inline" ).fadeOut( "slow" );
});
<p>p - <span>Scroll happened!</span></p>
span { display: none; }

Library

Lock

The mouse wheel will not propagate to the parent avoiding that the page is going upward when in a textarea

http://mohammadyounes.github.io/jquery-scrollLock

Documentation / Reference