javascript 启动 touchend 事件 - jQuery Mobile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13494100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:57:23  来源:igfitidea点击:

initiating a touchend event - jQuery Mobile

javascriptjqueryjquery-mobile

提问by Yanipan

I'm using jQuery mobile for my application.

我正在为我的应用程序使用 jQuery Mobile。

Is it possible to release a gesture using jQuery Mobile? If a user places fingers and drag the screen - can I release this gesture using JavaScript, even if he leaves his finger on the screen?

是否可以使用 jQuery Mobile 释放手势?如果用户放置手指并拖动屏幕 - 我是否可以使用 JavaScript 释放此手势,即使他将手指留在屏幕上?

For example - is it possible to release touch move after 1000 ms so the event will end, such as $(this).touchend();(like I could do $(this).click();) ?

例如 - 是否可以在 1000 毫秒后释放触摸移动以便事件结束,例如$(this).touchend();(就像我可以做的那样$(this).click();)?

Edit:On the other hand, maybe someone has any other idea on how to limit the touch/ touchmovetime?

编辑:另一方面,也许有人对如何限制touch/touchmove时间有任何其他想法?

回答by Alejo

You can trigger any event by calling $(this).trigger(eventName)See the jQuery docson this method.

您可以通过调用$(this).trigger(eventName)有关此方法的jQuery 文档来触发任何事件。

Your case would be: $(this).trigger('touchend');

你的情况是: $(this).trigger('touchend');

If you want an event that will fire after 1000ms of a touch, maybe consider the tapholdevent from jQM? You can specify the delay by setting $.event.special.tap.tapholdThreshold.

如果您想要一个在触摸 1000 毫秒后触发的事件,也许可以考虑来自 jQM的taphold事件?您可以通过设置来指定延迟$.event.special.tap.tapholdThreshold

回答by Phill Pafford

Does something like this work?

这样的东西有用吗?

JS

JS

function bindEventTouch(element) {
    element.bind('tap taphold swipe swiperight swipeleft', function(event, ui) {
        console.log('Event: '+event.type); 

        if(event.type == 'taphold') {
            console.log('Was Event taphold?: '+event.type); 
            element.trigger('touchend');
        }
    });

    element.bind('touchend', function(event, ui) {
        console.log('Triggered touchend Event: '+event.type); 
    });
}

$('.displayEvent').each(function(){
    bindEventTouch($(this));
});?

HTML

HTML

<div data-role="page" id="home"> 
    <div data-role="content">
        <div id="display-event-1" class="add-border displayEvent">
            <p>Tap, TapHold (for 1 second), Swipe, SwipeRight or SwipeLeft</p>
        </div>
        <br />
        <div id="display-event-2" class="add-border displayEvent">
            <p>Tap, TapHold (for 1 second), Swipe, SwipeRight or SwipeLeft</p>
        </div>
        <br />
        <div id="display-event-3" class="add-border displayEvent">
            <p>Tap, TapHold (for 1 second), Swipe, SwipeRight or SwipeLeft</p>
        </div>
    </div>
</div>?

CSS

CSS

.add-border {
    border-style:solid;
    border-width:1px;
    width:100%;   
    text-align:center;
}?