Disable carousel overscroll/overdrag in Sencha Touch

前端 未结 1 1434
故里飘歌
故里飘歌 2021-02-06 18:24

At the end or beginning of a Sencha Touch 2 carousel, a user can drag the item past where it should be able to go and display the white background (screenshot here: http://i.img

1条回答
  •  迷失自我
    2021-02-06 18:39

    What you need to do is override the onDrag functionality in Carousel. This is where the logic is to detect which direction the user is dragging, and where you can check if it is the first or last item.

    Here is a class that does exactly what you want. The code you are interested in is right at the bottom of the function. The rest is simply taken from Ext.carousel.Carousel.

    Ext.define('Ext.carousel.Custom', {
        extend: 'Ext.carousel.Carousel',
    
        onDrag: function(e) {
            if (!this.isDragging) {
                return;
            }
    
            var startOffset = this.dragStartOffset,
                direction = this.getDirection(),
                delta = direction === 'horizontal' ? e.deltaX : e.deltaY,
                lastOffset = this.offset,
                flickStartTime = this.flickStartTime,
                dragDirection = this.dragDirection,
                now = Ext.Date.now(),
                currentActiveIndex = this.getActiveIndex(),
                maxIndex = this.getMaxItemIndex(),
                lastDragDirection = dragDirection,
                offset;
    
            if ((currentActiveIndex === 0 && delta > 0) || (currentActiveIndex === maxIndex && delta < 0)) {
                delta *= 0.5;
            }
    
            offset = startOffset + delta;
    
            if (offset > lastOffset) {
                dragDirection = 1;
            }
            else if (offset < lastOffset) {
                dragDirection = -1;
            }
    
            if (dragDirection !== lastDragDirection || (now - flickStartTime) > 300) {
                this.flickStartOffset = lastOffset;
                this.flickStartTime = now;
            }
    
            this.dragDirection = dragDirection;
    
            // now that we have the dragDirection, we should use that to check if there
            // is an item to drag to
            if ((dragDirection == 1 && currentActiveIndex == 0) || (dragDirection == -1 && currentActiveIndex == maxIndex)) {
                return;
            }
    
            this.setOffset(offset);
        }
    });
    

    0 讨论(0)
提交回复
热议问题