Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect scrolling on pages without a scroll bar

Tags:

javascript

css

I have a webpage where the body has css

body {
  overflow-x: hidden;
  overflow-y: hidden;
}

I do not want to show the scroll bar.

I want to run some javascript when the user scrolls down, so I did this:

<script>
document.addEventListener("scroll", function (e) {
    console.log("scrolling engage");
    if (e.detail > 0) {
        // do things ...
    }
    return false;
}, true);
</script>

I also tried setting onscroll to the function. The problem is that this event does not fire, I then found this page which says the event only fires when there is a scroll bar, however I don't want a scroll bar.

How do I detect the scroll wheel without showing the scroll bar? I do not want to use jQuery.

like image 689
Alice Ryhl Avatar asked Jun 17 '15 13:06

Alice Ryhl


People also ask

How do I enable pop up scrolling?

Solution #1: Disable "Fixed Popup" Setting To make sure your popup scrolls properly, you can first disable the Fixed Positioning setting. In the Popup Editor, go to Popup Settings > Display > Position. Uncheck the Fixed Positioning option.

How do I know if I am scrolling to the bottom?

If you want to check whether the user has scrolled to the bottom of the page, you can use the scroll() jQuery event. The given code piece takes the top scroll of the window, so how much the page is scrolled down, it adds the height of the visible window and checks if it is equivalent to the height of the document.

How do I know if scroll is visible?

type == 'mouseenter') { if( ... if scrollbar visible ? ... ) { alert('true'): } else { alert('false'): } } });


1 Answers

EDIT: The previous version of this answer was for the mousewheel event that is non-standard/deprecated. Use the wheel event instead. Notice that in this case, the value of deltaY is opposite to the one of wheelDeltaY so instead of adding, you'll substract.

You can use the wheel event. Once that event is called, it has the Delta values that will show you how many pixels the mousewheel scrolled. Although you'll be interested only in the deltaY one.

Now, once you have the vertical change, you just need to update the translation of the body by updating the value of transform:translateY and substracting the e.deltaY value to it. Something like this:

document.addEventListener("wheel", function (e) {

    // get the old value of the translation (there has to be an easier way than this)
    var oldVal = parseInt(document.getElementById("body").style.transform.replace("translateY(","").replace("px)",""));

    // to make it work on IE or Chrome
    var variation = parseInt(e.deltaY);
    
    // update the body translation to simulate a scroll
    document.getElementById("body").style.transform = "translateY(" + (oldVal - variation) + "px)";

    return false;
    
}, true);
body {
    overflow-y:hidden;
    overflow-x:hidden;
}
<body id="body" style="transform:translateY(0px)">
  <p>1</p>
  <p>2</p>
  <p>3</p>
  <p>4</p>
  <p>5</p>
  <p>6</p>
  <p>7</p>
  <p>8</p>
  <p>9</p>
  <p>10</p>
  <p>11</p>
  <p>12</p>
  <p>13</p>
  <p>14</p>
  <p>15</p>
  <p>16</p>
  <p>17</p>
  <p>18</p>
  <p>19</p>
  <p>20</p>
</body>

It is difficult to see the effect on that window, You can see it better on this JSFiddle: http://jsfiddle.net/pLb93eeL/4/

like image 84
Alvaro Montoro Avatar answered Sep 21 '22 19:09

Alvaro Montoro