0

I am trying to detect scroll direction only when the user is at the canvas area.
I tried to replace "#myCanvas" with "canvas" but its not working.
Just to be sure when I change it to documnet its works. How can I make it work?

<canvas id="myCanvas" width="1400" height="800"></canvas>


var lastScrollTop = 0;
$("#myCanvas").on('scroll', function() {

    st = $(this).scrollTop();
    if(st < lastScrollTop) {
        console.log('up 1');
    }
    else {
        console.log('down 1');
    }
    lastScrollTop = st;
});
arii
  • 143
  • 2
  • 12
  • does you canvas have a scrollbar? i think it requires to have a scrollbar for scroll event to fire http://www.w3schools.com/jsref/event_onscroll.asp – AthMav Oct 08 '16 at 17:22
  • I don't have scrollbar in the canvas. is there other way I can detect the scroll direction only if the mouse is on the canvas? – arii Oct 08 '16 at 17:25
  • 1
    see this answer for more info.. http://stackoverflow.com/questions/15385290/canvas-scrollbar-not-working – Md. Khairul Hasan Oct 08 '16 at 17:25
  • 1
    You probably want to listen on the `wheel` event ([Get mouse wheel events in jQuery?](http://stackoverflow.com/questions/8189840/get-mouse-wheel-events-in-jquery)) that will tell you that the scroll wheel is used (or trackpad) and not the `scroll` event that will report if the content of an element is scrolled. – t.niese Oct 08 '16 at 17:29
  • @t.niese, you right, this is what I want – arii Oct 08 '16 at 17:32
  • So this can be marked as duplicate to the given question? – t.niese Oct 08 '16 at 17:32
  • @t.niese, solved, tnx – arii Oct 08 '16 at 17:50

0 Answers0