Bubbling scroll/mousewheel event - javascript

I've setup my app/website such that I have an absolute-positioned canvas element on top of a scrollpanel, when the scrollpanel scrolls I apply on offset to the canvas to make it look like the image is scrolling (this allows me to have huge canvas without the overhead of a huge canvas element). The problem is, when my mouse is over the canvas element, the scroll wheel does not work, since the scroll event does not bubble. In this case, however, I need the bubbling to get the scrollbar to work.
I'm using GWT for this, so I'd prefer not to rely on a jQuery solution (although a pure javascript solution would be ok) since it's kinda hard to mix the two. I can capture the mousewheel event, but the main problem with that is that it doesn't seem to differentiate between scrolling (up/down) and tilting of the wheel (left/right). I tried eventGetShiftKey(), eventGetButton(), eventGetType(), and some others but all those methods return the same exact result for scrolling and tilting (tilt left = scroll up, tilt right = scroll down).
It seems like the best way to handle this is to bubble the actual event to the scrollpanel (which by the way also contains the parent div that contains the absolute-positioned canvas), but I'm not sure if that's possible?

Mousewheel event does bubble, to differentiate between up/down scrolling use the event.wheelDelta and event.detail attributes.
MSDN: onmousewheel Event (IE, WebKit)
event.wheelDelta indicates the distance that the wheel button has rotated, expressed in multiples of 120. A positive value indicates that the wheel button has rotated away from the user. A negative value indicates that the wheel button has rotated toward the user.
MDC: DOMMouseScroll (Gecko)
event.detail specifies the number of "ticks" that the mouse wheel moved. Positive values mean down/right", negative up/left.
event.axis specifies the axis of the scroll gesture (horizontal or vertical). This attribute was added in Firefox 3.5
Also see this article which talks a bit about normalizing.

Related

Calculate mouse wheel scroll speed using JavaScript

I need some help computing and calculating mouse wheel scrolling speed.
I want to determine when the user is scrolling using their mousewheel and how fast or slow they scrolling.
Based on speed I want to do some operations.
How can I find mousewheel scrolling speed?
I've looked into this before, and there are three issues here.
Mouse wheel speed varies in how it is reported by the browser.
User preferences determine what impact each 'click' of a scroll has on a site.
There are some mouses that don't 'click' and instead spin freely, which gives weird numbers.
The combination of these three make this a rather useless metric to do anything useful with. One person could scroll 5 times and be at the bottom of your site, while another 30 times and only advanced one paragraph.
In my case, I use deltaY as a value. deltaY could be negative(scroll top) or positive(scroll down).
Convert the value to only positive number using Math.abs(event.deltaY) and you can use that as a speed.

Javascript: detect OS X "natural scroll" settings

I'm facing an issue. For a project I'm doing I'm detecting the scrollwheel position and based on that I'm navigating to the next slide or not. A problem is, however, that some Mac users use "natural scroll" - inverting their scrolling on pages. This means that, for those users, I should use scroll in the other direction as trigger.
My question is; is there a way to detect in what direction the user is used to scroll? My initial idea was to track scrolling and see how scrollTop and scrollwheel relate to each other (i.e., I record mousewheel events and see which direction the page scrolls as a result). That, however, requires the user to scroll before I know what to do. Which doesn't work, as users first need to trigger a slide change.
I'm at a loss. All help is appreciated.
There's actually an easy answer, as long the Mac users are using Safari--
function myWheelEventHandler(event) {
var deltaY = -event.wheelDeltaY;
if (event.webkitDirectionInvertedFromDevice) deltaY = -deltaY;
// use value for something
}
In this example, the value of deltaY will be positive when the user rolls the mouse wheel away from them (or the trackpad equivalent), and negative otherwise, regardless of the system-wide "natural" scroll setting.
In other words, if the webkitDirectionInvertedFromDevice property is present and has the value true, then you can be sure "natural" scrolling is enabled. It even updates if the setting changes while your script is running. The property is available for wheel events only (not scroll events).
If the property is not present, or is present but has the value "false" (which will always be the case in Chrome, due to a bug), then unfortunately you don't know if the scroll direction is reversed or not.
Your idea of testing to see how the page moves on wheel events may be the most robust solution. You could create an invisible (empty) div in front of your slideshow, set to overflow:scroll, with a taller empty div inside it. The first time you receive a wheel event on this div, you could then work out the scroll direction and trigger the appropriate slide change.

Javascript image gallery for mobile

I want to make an image gallery for mobile with similar functionality to Flickr.
I'd like for it to move an image on touchmove depending on the direction your finger moves, and if you make a small swipe gesture, I'd like it to move the image all the way over, or if the swipe wasn't big enough, move the div back to its starting position on the x axis.
I know what the logic would look like, I'm just unsure of the syntax to use.
I think it'd be something like this:
On touchstart
-Assign variable for x position of touchstart
On touchmove
-Assign variable for x position of touchmove
-assign a variable for touchmove minus touchstart
- add (touchmove minus touchstart) to the current x position of a div
On touchend
- if touchmove => touchstart + 50px
then add 1000px - (touchmove-touchstart) to the divs x position
-else if touchmove =< touchstart - 50px
then minus 1000px + (touchmove - touchstart) from the divs x position
Else
Minus (touchmove - touchstart) from the x position of the div
Can anyone translate this^^^ to javascript?
If you are really up to the challenge, there is a lot of things to consider. You will need to make it platform independent, decide whether to animate with JavaScript, CSS3 animations or a bit of both. It will need to be responsive and allow the images to be loaded on demand etc. What about backwards compatibility?
You will also need to work in some clever logic that doesn't just detect the distance between the touchstart and touchmove but also the speed this was done. For example the user could flick their finger a short distance very fast causing a transition to occur or could move their finger a long distance very slowly causing a transition to not occur (within limits).
So to answer your question, no this will not just animate by itself. It will not move smoothly without some clever and efficient JavaScript or CSS3 animations.
I would recommend using one of the MANY JQuery libraries that already exist to do this for you. There are a number of free libraries out there that work great. After a quick search I found this one:
http://www.photoswipe.com/, which seems to do exactly what you are trying to achieve.

JavaScript - How to stop mouse movement

No jQuery possible.
How to disable / stop mouse movement with JavaScript?
What to do: If the mouse is being moved to, let's say, position left < 300, disable ouse movement to prevent further moving into this direction.
Is that possible?
The Pointer Lock API might be what you're looking for
https://developer.mozilla.org/en-US/docs/API/Pointer_Lock_API
Javascript can read mouse position but not set it. The mouse cursor properties exist outside the HTML Document Object Model and thus are beyond the reach of Javascript. Mouse events however are captured and thus readable / event modification also permissible (mouse hover, mouse over, mouse out, mouse click, dblclick).
You cannot do this with JavaScript and I dare say you never will be able to.
If you need to do this for some kind of game element, for example, I recommend that you place a page-element under the mouse and prevent the page-element from moving outside of the bounds even if the mouse does.

Two-fingered scroll js on touchpad

We have developed a site whcih has a horizontal orientation and are wanting to implement touchpad control with two fingers move left/right.
When you move two fingers left/right on touchpad, the site page is being scrolled left/right. Now we have implemented touchpad control with two fingers move up/down and page scrolled left/right.
How can we change touchpad control with two fingers move from up/down to left/right to scroll site page left/right using js or jQuery?
I may be a little late but had the same question before I stumbled over this question.
A little further investigation lead me to think that the best bet to capture trackpad scrolling would be the wheel event.
function doScroll(e) {
// positive deltas are top and left
// down and right are negative
// horizontal offset e.deltaX
// vertical offset e.deltaY
console.log(`x:${e.deltaX} y:${e.deltaY}`);
e.preventDefault(); // disable the actual scrolling
}
window.addEventListener("wheel", doScroll, false);
I have prepared a fiddle that tells you the scroll direction and offset values but prevents the scrolling itself.
The wheel event has a delta property that (at least in Chrome) is sensitive to momentum and gives you the current relative scroll offset rather than the absolute scroll position available in the scroll event.
Usually when you want to take over touch events in script, you add something like this to prevent the usual scroll and zoom:
$("body").bind("touchstart", function(e) {
e.preventDefault();
})
What you need to do is change what can be scrolled. If your page is big enough where left/right scrolling makes sense, the browser will allow it be scrolled that way.
Basically, if you only want them scrolling in a certain direction, only make content in that direction. If necessary, you can achieve this by having a container div of the specific size you want with overflow set to none.

Categories