52

I'm trying to implement a simple "stay inside the viewport" behaviour to a div via jquery. For that i need to bind a function to the scroll event of the window, but i can't seem to get it to fire up: nothing happens. I've tried a simple alert(), console.log() no dice. An idea what i'm doing wrong?

This code :

$(window).scroll(function () {  
            console.log("scrolling");           
});

sits in script.js, located at the very bottom of my html file

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="js/script.js"></script>
</body>
</html>

UPDATE Test URL: http://pixeline.eu/test/menu.php

mins
  • 6,478
  • 12
  • 56
  • 75
pixeline
  • 17,669
  • 12
  • 84
  • 109
  • 1
    Is the content of the page long enough? Works perfectly: http://jsfiddle.net/fkling/x5NzM/ If there is no content that can be scrolled, the `scroll` event won't be raised. – Felix Kling Apr 16 '11 at 12:59
  • your example page is binding to `$(document).scroll` try `$(window).scroll` – MikeM Apr 16 '11 at 13:42
  • 1
    it seems that the the scroll not triggering has something to do with the way your using the css, am doing some tests now. – Charlie Sheather Apr 16 '11 at 13:45
  • 3
    Also finding that having `overflow-x` on the body tag caused the scroll events to not fire either! Reproduced in both Chrome and Firefox. – BU0 Sep 19 '18 at 21:14
  • @BU0 I have also found that to be the case ( in fact your comment just solved a perplexing problem I was having). Thanks. – Cheryl Velez May 30 '19 at 14:46
  • Make sure you're calling your script below jquery cdn. – Adnan Siddique Jan 24 '23 at 15:22

8 Answers8

125

Your CSS is actually setting the rest of the document to not show overflow therefore the document itself isn't scrolling. The easiest fix for this is bind the event to the thing that is scrolling, which in your case is div#page.

So its easy as changing:

$(document).scroll(function() {  // OR  $(window).scroll(function() {
    didScroll = true;
});

to

$('div#page').scroll(function() {
    didScroll = true;
});
OGHaza
  • 4,795
  • 7
  • 23
  • 29
Charlie Sheather
  • 2,374
  • 6
  • 20
  • 21
39

My issue was I had this code in my css

html,
body {
    height: 100%;
    width: 100%;
    overflow: auto;
}

Once I removed it, the scroll event on window fired again

digitalzoomstudio
  • 1,112
  • 1
  • 15
  • 30
  • 3
    I had the same problem here. Looks like the `scroll` event doesn't handles well an element with css `overflow`. That was bugging even animations with `scrollTop`... Thanks for sharing! – Clyff Jun 09 '16 at 13:36
  • For future reference, trying to use materializecss's pushpin component (http://materializecss.com/pushpin.html), in my case, wasn't working because of height:100% on html and body as well! – StinkyCat Feb 13 '17 at 15:21
5
$('#div').scroll(function () {
   if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight-1) {

     //fire your event


    }
}
Raptor
  • 53,206
  • 45
  • 230
  • 366
Ashish Asodiya
  • 91
  • 1
  • 10
4

The solution is:

 $('body').scroll(function(e){
    console.log(e);
});
Pan Bouradas
  • 179
  • 1
  • 6
2

To whom its just not working to (like me) no matter what you tried: <element onscroll="myFunction()"></element> works like a charm

exactly as they explain in W3 schools https://www.w3schools.com/tags/ev_onscroll.asp

0

Nothing seemd to work for me, but this did the trick

$(parent.window.document).scroll(function() {
    alert("bottom!");
});
LegionDev
  • 1,391
  • 4
  • 15
  • 29
0

In my case you should put the function in $(document).ready

$(document).ready(function () {
    $('div#page').on('scroll', function () {
     ...
    });
});
Ahmad
  • 8,811
  • 11
  • 76
  • 141
-3
  1. Declare your jQuery between <script> and </script> in the <head>,
  2. Wrap your .scroll() event within
$(document).ready(function(){
  //do something
});
Always Helping
  • 14,316
  • 4
  • 13
  • 29
MikeM
  • 27,227
  • 4
  • 64
  • 80