jquery-scroll icon indicating copy to clipboard operation
jquery-scroll copied to clipboard

Mobile

Open tsukasa1989 opened this issue 12 years ago • 11 comments

Hello,

I needed some mobile scrolling and made the following code.

Functions to add:

onTouchStart : function(e){
        this.touch = {
            start : {
                y : e.touches[0].pageY
            }
        };
    },

    onTouchMove: function(e){
        // calculate new handle position
        this.handle.top += (e.touches[0].pageY - this.touch.start.y) * 0.01;

        this.setHandlePosition();
        this.setContentPosition();

        // prevent default scrolling of the entire document if handle is within [min, max]-range
        if(this.handle.top > this.props.handlePosition.min && this.handle.top < this.props.handlePosition.max){
            e.preventDefault();
        }
    },

Add the following code to the function appendEvents:

// Touch events
        var _this = this;
        this.container[0].addEventListener("touchstart", function(e){
            _this.onTouchStart(e);
        }, false);

        this.container[0].addEventListener("touchmove", function(e){
            e.preventDefault();
            _this.onTouchMove(e);
        }, false);

Good luck!

Tsukasa

tsukasa1989 avatar Mar 02 '12 11:03 tsukasa1989

hello there, I was wondering if this supports touch scrolling in iOS as well?

sincero avatar May 20 '12 18:05 sincero

I have tested this on Android 3.2, 4.0, an iPhone 4S and a Samsung Galaxy Tab 10.1 with Android 3.2.

And it works on all. So it should support all touch devices. As long as they trigger the touchemove and touchstart events in Javascript.

tsukasa1989 avatar May 21 '12 10:05 tsukasa1989

sweet! one more question, where does the first piece of code go? The second piece of code i got, but i wasn't exactly sure where to paste the first part

sincero avatar May 22 '12 03:05 sincero

You can put that before:

    //
    // mouse wheel movement
    //
    onMouseWheel: function(ev, delta){

tsukasa1989 avatar May 23 '12 16:05 tsukasa1989

could you show us the example working? thanks!

uekeueke avatar May 31 '12 14:05 uekeueke

I got a live example over here: http://www.shirtbyhand.co.uk/configurator_html5/

Just click through it and when you get to Fabrics on the left hand side you will get a scrollbar.

Good luck!

tsukasa1989 avatar May 31 '12 19:05 tsukasa1989

tsukasa1989, you are a lifesaver. Your code has mobile support plus it supports scrolling on mouse focus! This should be included in the code already. Thanks again!

gr82d8 avatar Jul 03 '12 03:07 gr82d8

Hi tsukasa,

I think there is actually a bug in the way the new position is calculated - this adjusts the position whenever the touchMove occurs, so that the touch position difference sets the velocity for the move, not the new position.

For a quick example, try the following:

  1. Touch screen
  2. Drag up vertically
  3. Start moving finger left and right
  4. Vertical scrolling will continue

You can also try scrolling down and back up, it always scrolls in one direction. I ended up implementing following which I think should be correct:

// Save the start position of handle
start : {
    y : e.touches[0].pageY,
    handleTop : this.handle.top
},
// Handle position based on displacement from touch start
var movement = e.touches[0].pageY - this.touch.start.y;
this.handle.top = this.touch.start.handleTop - movement*0.25;

thedustyrover avatar Aug 04 '12 00:08 thedustyrover

Thanks alot! is it working?

tsukasa1989 avatar Aug 04 '12 07:08 tsukasa1989

Yes, it's working on the implementation I have. I also encountered some bugginess with clicks being registered after the scroll in iOS. I ended up implementing some code to intercept the mouse events, detect the drag distance to determine whether it should be considered a scroll gesture or not, and then re-triggering mouse events if it was not. I think there is probably a better solution out there though.

thedustyrover avatar Aug 06 '12 17:08 thedustyrover

Hi dailynathan. where should the bugfixes go should it be after this: // // set position of handle // setHandlePosition: function(){

        // stay within range [handlePosition.min, handlePosition.max]
        this.handle.top = (this.handle.top > this.props.handlePosition.max) ? this.props.handlePosition.max : this.handle.top;
        this.handle.top = (this.handle.top < this.props.handlePosition.min) ? this.props.handlePosition.min : this.handle.top;

        this.handle[0].style.top = this.handle.top + 'px';
    },

Thanks!

gr82d8 avatar Sep 03 '12 03:09 gr82d8