Scroll Diffrence

My Question is straight. Is there any way to detect that the scroll to a page has been due to javascript or mouse scroll. I really need to identify the difference.

Is there anybody who can help me to figure out the difference between the scroll made by mouse of a user or it has been due to jQuery or java script scroll event

I am working on a co browsing app, so there is transfer of events among multiple users. I am able to manage all the events except scroll. It lets the system to infinite scroll if scrolling from agent.html is recorded. you can see the app by opening the urls 182.71.103.93/screen2/client23122014.html and then 182.71.103.93/job_tree

Upvotes: 0

Views: 83

Answers (2)

Answer give by Termhn was good enough but if anyone stuck to the similar situation as of mine then you may use a global javascript variable

i did it in a way

For Client / user side

var emit_scroll_event=true;

socket.on('agentwindowscroll',function (msg){emit_scroll_event=false;  jQuery(document).scrollTop(msg);   });

//window scroll logic goes here
jQuery(document).scroll(function()
{
    var scrollFromTop=jQuery(document).scrollTop();
    if(emit_scroll_event)
    {
    socket.emit('windowscroll', scrollFromTop);
    }
    emit_scroll_event=true;
});

For Agent side we may use similar code

var emit_scroll_event=true;
//agent window scroll logic goes here
jQuery(document).scroll(function()
{
    var scrollFromTop=jQuery(document).scrollTop(); 
    if(emit_scroll_event)
    {
    socket.emit('agentwindowscroll', scrollFromTop);
    }
    emit_scroll_event=true;
});
 //responding to client scroll
  socket.on('windowscroll',function (msg){emit_scroll_event=false;   jQuery(document).scrollTop(msg);   });

Note: This is not entire code. It is just the part of code that i used which helped me to sort out mine issue. It is not for normal javascript. It is used with Node js with Scoket.io module

Upvotes: 0

Termhn
Termhn

Reputation: 129

Not exactly what you're asking but this will detect a mouse wheel event and therefore if it's not a mousewheel event it's caused by JS. You can use the "mousewheel" ("DOMMouseScroll" in Firefox) event in JS. Example:

// Chrome/Safari/Opera/New IE
$('html','body').addEventListener("mousewheel", MouseWheelHandler, false);

// Firefox
$('html','body').addEventListener("DOMMouseScroll", MouseWheelHandler, false);

// Old IE
$('html','body').addEventListener("onmousewheel", MouseWheelHandler, false);

var MouseWheelHandler = function(e) {
  var e = window.event || e; //IE support
  var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));

  // Do whatever with the delta value
}

Upvotes: 1

Related Questions