【问题标题】:How to swipe top down JQuery mobile如何自上而下滑动 JQuery 移动
【发布时间】:2017-04-27 15:22:55
【问题描述】:

我正在尝试通过上下滑动而不是左右滑动来制作事件

我有这个卷,如图所示:

我可以使用箭头图标 (onClick()) 处理事件,但我想添加向上滑动事件, 添加滑动事件时,它适用于左右 我希望它在图像显示时向上 有什么帮助吗?

【问题讨论】:

    标签: javascript jquery css html jquery-mobile


    【解决方案1】:

    jQuery Mobile 原生为我们提供了捕捉向左滑动和向右滑动的能力。但是,它并没有为我们提供开箱即用的向上和向下滑动。调整 jQuery 团队为 swipeleft 和 swiperight 所做的工作,我们能够以相同的方式创建和捕获这些事件。看下面的代码实现swipeup和swipedown:

    (function() {
        var supportTouch = $.support.touch,
                scrollEvent = "touchmove scroll",
                touchStartEvent = supportTouch ? "touchstart" : "mousedown",
                touchStopEvent = supportTouch ? "touchend" : "mouseup",
                touchMoveEvent = supportTouch ? "touchmove" : "mousemove";
        $.event.special.swipeupdown = {
            setup: function() {
                var thisObject = this;
                var $this = $(thisObject);
                $this.bind(touchStartEvent, function(event) {
                    var data = event.originalEvent.touches ?
                            event.originalEvent.touches[ 0 ] :
                            event,
                            start = {
                                time: (new Date).getTime(),
                                coords: [ data.pageX, data.pageY ],
                                origin: $(event.target)
                            },
                            stop;
    
                    function moveHandler(event) {
                        if (!start) {
                            return;
                        }
                        var data = event.originalEvent.touches ?
                                event.originalEvent.touches[ 0 ] :
                                event;
                        stop = {
                            time: (new Date).getTime(),
                            coords: [ data.pageX, data.pageY ]
                        };
    
                        // prevent scrolling
                        if (Math.abs(start.coords[1] - stop.coords[1]) > 10) {
                            event.preventDefault();
                        }
                    }
                    $this
                            .bind(touchMoveEvent, moveHandler)
                            .one(touchStopEvent, function(event) {
                        $this.unbind(touchMoveEvent, moveHandler);
                        if (start && stop) {
                            if (stop.time - start.time < 1000 &&
                                    Math.abs(start.coords[1] - stop.coords[1]) > 30 &&
                                    Math.abs(start.coords[0] - stop.coords[0]) < 75) {
                                start.origin
                                        .trigger("swipeupdown")
                                        .trigger(start.coords[1] > stop.coords[1] ? "swipeup" : "swipedown");
                            }
                        }
                        start = stop = undefined;
                    });
                });
            }
        };
        $.each({
            swipedown: "swipeupdown",
            swipeup: "swipeupdown"
        }, function(event, sourceEvent){
            $.event.special[event] = {
                setup: function(){
                    $(this).bind(sourceEvent, $.noop);
                }
            };
        });
    
    })();
    

    这是来自Blackdynamo的答案

    【讨论】:

    • 如何设置swipeupdown事件的监听器并为swipeup和swipedown事件执行不同的任务?
    • @SantoshGhimire,你可以使用$('mySelector').on('swipeup', function() {});$('mySelector').on('swipedown', function() {});
    • 我使用了这个 jQuery 插件,它是最简单的。 github.com/mattbryson/TouchSwipe-Jquery-Plugin
    • 此答案不起作用,如果滑动的开始和目标不是同一个 div:请参阅下面的答案进行修复!小提琴:jsfiddle.net/Q3d9H/124
    • 我只是想补充一下。答案仍然很完美。我将此脚本添加到头部,并使用了“$('.myClassSelector').on('swipedown', function() {});”。在第一次尝试中工作。谢谢。
    【解决方案2】:

    我在这里接受的答案有问题,因为当滑动的起点和目标不同时,没有检测到滑动。

    这可能是一个更简单的答案,我直接覆盖 jquery handleSwipe 事件(基于 jquery.mobile-1.4.5)并通过垂直滑动附加它,向上和向下调用:

    (function( $, window, undefined ) {
    
        //custom handleSwipe with swiperight, swipeleft, swipeup, swipedown
        $.event.special.swipe.handleSwipe = function( start, stop, thisObject, origTarget ) {
            if ( stop.time - start.time < $.event.special.swipe.durationThreshold ) {
                var horSwipe = Math.abs( start.coords[0] - stop.coords[0] ) > $.event.special.swipe.horizontalDistanceThreshold;
                var verSwipe = Math.abs( start.coords[1] - stop.coords[1] ) > $.event.special.swipe.verticalDistanceThreshold;
                if( horSwipe != verSwipe ) {
                    var direction;
                    if(horSwipe)
                        direction = start.coords[0] > stop.coords[0] ? "swipeleft" : "swiperight";
                    else
                        direction = start.coords[1] > stop.coords[1] ? "swipeup" : "swipedown";
                    $.event.trigger($.Event( "swipe", { target: origTarget, swipestart: start, swipestop: stop }), undefined, thisObject);
                    $.event.trigger($.Event( direction, { target: origTarget, swipestart: start, swipestop: stop }), undefined, thisObject);
                    return true;
                }
                return false;
            }
            return false;
        }
    
        //do binding
        $.each({
            swipeup: "swipe.up",
            swipedown: "swipe.down"
        }, function( event, sourceEvent ) {
            $.event.special[ event ] = {
                setup: function() {
                    $( this ).bind( sourceEvent, $.noop );
                },
                teardown: function() {
                    $( this ).unbind( sourceEvent );
                }
            };
        }); 
    })( jQuery, this );
    

    【讨论】:

    • “接受的答案完美无缺”,50 人在这个社区投票赞成
    • 尝试从底部 div 滑动到顶部 div,不起作用:jsfiddle.net/Q3d9H/124 我的解决方案可以处理(小提琴很复杂,因为我扩展了 jquery 脚本)
    • 在你说之前,这是想要的行为,不,不是。 Jquery swipeleft 和 swiperight 还可以正确处理不同的开始和目标 div。所以我的新解决方案是真正正确的解决方案,没有任何错误。尽管您链接中的简单示例运行良好,但它无法处理可能的常见用例。最好的问候
    猜你喜欢
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多