I am creating a web application specifically designed for phones (primarily iPhone, but Android and WP are on the horizon ...).
One of the screens contains a scrollable list of items. I would like the list to behave similarly to the built-in iOS Mail application.
In other words...
- If the user touches the list and moves up or down, the list scrolls vertically.
- If the user clicks up or down, the list scrolls vertically with a natural momentum
- If the user touches the list and moves ONLY to the left - a specific element moves to the left, opening the delete button.
- IMPORTANT - the list should scroll, OR , the item should slide , BUT NEVER ANYTHING .
So, itβs important to find out what the user's intention is, which means that I probably need to prevent ANY response until I find out if the user is moving the finger vertically or horizontally.
Just setting these CSS styles into a list container ...
overflow-y: auto; -webkit-overflow-scrolling: touch;
... I get # 1 and # 2 above. So, I need to figure out how to implement # 3.
My first thought was to implement something like this (pseudo code) ...
- Create a
touchstart event touchstart in the list container. In the callback, save the x- and y-coordinates of the custom touch start position. - Create a
touchmove event touchmove in the list container. In the callback, find out how far the user's finger moves (e.g. delta_x and delta_y) - If delta_x AND delta_y - less than 10 pixels - do nothing (do not scroll through the list or move the item) - since we have not yet figured out whether the user plans to move up / down or left / right.
- If EITHER delta_x OR delta_y is more than 10 pixels, we can assume that the user has moved far enough to express his intention. If delta_y> delta_x, suppose it moves up / down and allows you to scroll through the list, but does not move the item. If delta_x> delta_y, suppose it moves left / right, so we must allow the item to slide, but not allow scrolling of the list.
I expected that I would use event.preventDefault () in touchstart or touchmove to control when scrolling starts. For example.
div.addEventListener("touchstart", function(e) { touchStart = { x: e.touches[0].pageX, y: e.touches[0].pageY } }, false); div.addEventListener("touchmove", function(e) { touchNow = { x: e.touches[0].pageX, y: e.touches[0].pageY } var dx = touchStart.x - touchNow.x, dy = touchStart.y - touchNow.y; if ((Math.abs(dx) < 10) && (Math.abs(dy) < 10)) {
However - this does not work. No matter how far you move, the list NEVER scrolls.
Obviously - I don't understand what triggers the scroll, and what the .preventDefault () event should do in this context.
So - is there a way to accomplish what I need?
I am hoping for a clean JavaScript solution (so I understand it better), but the jQuery approach will be fine. I definitely hope to avoid the jQuery plugin / library, if at all possible ...
Thanks in advance!
javascript jquery ios web-applications webkit
mattstuehler
source share