Rein Bentdal Rein Bentdal - 6 months ago 17
Javascript Question

scroll while bottom of div

I have some problem running a function while "scrolling = true". All i want to do is to be able to run a script, while scrolled to the bottom of a div.

function scrolled (o) {
//visible height + pixel scrolled = total height
if(o.offsetHeight + o.scrollTop == o.scrollHeight) {
var scrolling = true;
} else {
var scrolling = false;
alert(scrolling);
}
}

setInterval(function scrollNaa () {
if(scrolling==true); {
alert("test");
}
}, 1000)


Why isnt the last alert functioning and alerting "test"?

Answer

You have a superfluous semicolon after your if statement.

Also, you have to define scrolling as a global variable. Currently, it's only in the scope of the scrolled() function. This may help:

var scrolling;

function scrolled (o) {
  //visible height + pixel scrolled = total height 
  if(o.offsetHeight + o.scrollTop == o.scrollHeight) {
   scrolling = true;
  } else {
   scrolling = false;
    alert(scrolling);
  }
}

setInterval(function scrollNaa () {
  if(scrolling==true) {
    alert("test");
  }
}, 1000)

(I also removed the semicolon.)

Comments