0

In Javascript, I want to detect whether the user move the scroll bar or the scroll bar moved by script. I have write the code to move the div scrollbar using javascript. Here i want to differentiate the scroll position whether the user manually moved or it moved by script. I have used below code. But it detects all.

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

var logDiv = document.getElementById('log');

logDiv.addEventListener('DOMMouseScroll', function() {
    console.log("mouseEvent");
    userManualScroll = true; 
});

Pls help me.

cjmling
  • 6,896
  • 10
  • 43
  • 79
selvan
  • 1,183
  • 3
  • 16
  • 24

1 Answers1

3

Set a global variable

var userscroll = true;

Whenever you adjust scroll position via JS, set userscroll to false before the scroll operation and true afterwards.

In any functions that only trigger on the user scrolling, add an if-clause

if (userscroll) {
   ...
MiK
  • 918
  • 4
  • 16