Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if event was fired from scrollbar

I have a div in which I have implemented iPad like swiping.

Check out what I mean on JSFiddle (Just click and drag your mouse in the div.)

I want to prevent the swiping from happening when someone is using the scroll bar (click on the scrollbar, then move from left to right, swiping still occurs).

In short, what I need is if(!event.wasTriggeredFromScrollbar) that I can use in any event trigger (in this case mousedown/move/up).

jQuery and regular 'ol Javascript answers welcomed.

Edited: To make a lot more sense.

like image 787
MatthewKremer Avatar asked Oct 15 '11 17:10

MatthewKremer


People also ask

How do I know if I have scroll reached end?

scrollTop - elem. clientHeight; It calculates distance scroll bar to bottom of element. Equal 0, if scroll bar has reached bottom.

Which event does a scrollbar component cause?

When you scroll a document or an element, the scroll events fire. You can trigger the scroll events in the following ways, for example: Using the scrollbar manually.

How do I check if a scroll is present?

To check if a scrollbar is present for an element, we can compare the scrollHeight value with its clientHeight value, both are in pixels.

What is scroll event in Javascript?

The scroll event is sent to an element when the user scrolls to a different place in the element. It applies to window objects, but also to scrollable frames and elements with the overflow CSS property set to scroll (or auto when the element's explicit height or width is less than the height or width of its contents).


1 Answers

Here's how I got around this, as it doesn't seem to actually be possible.

Step 1: Capture the MouseDown event. Store the current position of scrollLeft and scrollTop into variables.

Step 2: On the MouseMove event, check the current position of scrollLeft and scrollTop and compare them against your variables. If they're different, then cancel whatever operation you were going to perform. If they're the same, the MouseDown event wasn't triggered by a scrollbar (or anything else that would change scrollLeft and scrollTop such as another animation), and you're free to do what you wish!

Happy coding!

like image 199
MatthewKremer Avatar answered Oct 29 '22 01:10

MatthewKremer