jQuery & Touch/Mobile: Perform Function on Swipe Left / Right - javascript

How can I achieve to let jQuery/JavaScript perform a jQuery-Function if a mobile-user swipes with one finger to the left an another function if he swipes to the right?
I like to animate scrollLeft and scrollRight to a certain position if the user swipes left/right. I'm doing the same for desktop with keyboard-navigation trough Arrow Left/Right which works perfectly. Now I need the same for swiping on touch devices.
(if possible without jQuery Mobile or another additional library)

ok heres a long answer but it worth it:
jQuery (Swipe vs. Touch) pageX and pageY keep returning 0
Hope it helps

Using some of the following events you can work out which direction a swipe has been made
touchstart: Occurs when a finger is placed on the screen
touchend: Occurs when a finger is removed from the screen
touchmove: Occurs when a finger already placed on the screen is moved across the screen
touchcancel: Occurs when a touch is cancelled before the finger is actually removed from the screen
Record the x,y position on touchstart and again on touchend and determine which direction a swipe has been made

Related

Aurelia animations based on events

How do you animate elements in Aurelia based on events? (i.e., touch events in the browser called touchstart, touchmove, touchend)
Let's take the Android Gmail app as an example for this question. You can archive certain messages by swiping a message to the right or left and you can extend the side menu by pressing the hamburger icon in the top left or start swiping on the left edge of the screen. These elements move depending how much you've moved your finger from the begin position. How would you do this in Aurelia?

Move/Slide Page To Left/Right Whe User Slide Left/Rigt With Finger On Touch Device

I wanted my webpage can change when user slide left/right using their finger on touch device (android/tablet). I found following source code that works what i need:
http://padilicious.com/code/touchevents/
But that has few limitations. It doesn't move/slide the page/element to left/right. It just change the page (href/page location). So, user don't understand whether they slide it correctly or not.
but on touch screen device like gallery you can see when user slide to left/right image move to left/right as user move the finger and it goes away left/right when user finish the slide.
i want to do something like that.
so, when user move finger my page will move with the finger too and when he remove the finger my page will go left/right with slide transition effect.
here is the sample code for page transition...
http://www.w3schools.com/jquerymobile/tryit.asp?filename=tryjqmob_trans_slide
hope that make sense..
You can use jQt to capture touch screen swipe events. After capturing these events you can easily make an application that navigates like you want. Also jQt has it's own webkit animations. And they are pretty cool.
You can also check this Safari Web Content Guide to capture these events yourself.
Another nice tutorial for detecting swipes for touch screens.

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.

Bubbling scroll/mousewheel event

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.

scroll with mouse wheel and mouse movement

I try to make a gallery that I can move in its content with mouse wheel or with mouse movement.
I used the following script for mouse movement http://valums.com/files/2009/menu/final.htm and for mouse wheel I use Mouse Wheel Plugin.
Separately they work great but when I try to combine them I have some problems. Check the following demo:
http://jsfiddle.net/A93mF/
As you can see partially works but, and I say partially because when I use mouse wheel to scroll it's ok, but when I move a bit the cursor then returns in the previous position.
How can I make it so, if I scroll and then move the cursor to continue normally the scrolling instead of return in the previous position?
Any solution is acceptable (change javascript[jQuery], html structure, plugin or whatever)
I rewrote the mouse movement script and now I think is works fine and in Chrome also (thanks #Nicola Peluchetti ) For anyone interested can check the following demo: http://jsfiddle.net/A93mF/3/

Categories