触摸事件在模式弹出窗口上不起作用

Touch events are not working on Modal Popup

本文关键字:窗口 不起作用 事件 模式 触摸      更新时间:2023-09-26

我在模态弹出窗口的触摸事件中遇到了一个简单而复杂的问题。在我的弹出窗口中,我显示了一张图像,对于该图像,我正在触发触摸事件,但它在一段时间内有效,并且几乎在所有时间都不起作用。第二个问题仅在模态弹出窗口上:滑动事件根本没有触发。可能有什么问题?以下是我在 Logcat 中收到的警告:对于模态弹出窗口的每一次触摸,我都会得到这个W/webview(5558):从 webcore 接收ACTION_DOWN过时的触摸事件;忽略

对于在模态弹出窗口上滑动,我得到:11-14 12:42:09.420:W/webview(5558):错过了一个拖拽,因为我们正在等待WebCore的响应。

有趣的是,仅在模态弹出窗口上发生,而不是所有屏幕。任何帮助将不胜感激

下面我用于模态弹出窗口的javascript代码

var modal = (function() {
var method = {}, $overlay, $modal, $content, $close;
// Center the modal in the viewport
method.center = function() {
    var top, left, position;
    top = Math.max($(window).height() - $modal.outerHeight(), 0) / 2;
    left = Math.max($(window).width() - $modal.outerWidth(), 0) / 2;
    $modal.css({
        top : top + $(window).scrollTop(),
        left : left + $(window).scrollLeft()
    });
};   
// Open the modal
method.open = function(settings) {
    $content.empty().append(settings.content);
    $modal.css({
        width : settings.width || 'auto',
        height : settings.height || 'auto'
    });
    method.center();
    $(window).bind('resize.modal', method.center);
    $modal.show();
    $overlay.show();
};   
// Close the modal
method.close = function() {
    // alert("Called close method");
    $modal.hide();
    $overlay.hide();  
    $content.empty();
    $(window).unbind('resize.modal');
};
// Generate the HTML and add it to the document
// $screen = $()
$overlay = $('<div id="overlay"></div>');
$modal = $('<div id="modal"></div>');
$content = $('<div id="content"></div>');
$close = $('<a id="close" href="#">close</a>');
$modal.hide();
$overlay.hide();
$modal.append($content, $close);
$(document).ready(function() {
    $('body').append($overlay, $modal);
//Here tried with image id, div id and modal BUT No work
document.getElementById("content").addEventListener( 'touchstart',
function(e){ onStart(e); }, false );
    function onStart ( touchEvent ) { 
            var flag = confirm("Are you sure want to defuse it?")
                if (flag == true) {                 
                $('#bombImg').attr('src', 'img/undefused.png');
            } else {   
                $('#bombImg').attr('src', 'img/bomb01.png');
            }
     touchEvent.preventDefault();
            modal.close();
      }
}); 
return method;
 }());   
 // Wait until the DOM has loaded before querying the document
//this method calling from another HTML file
 function showDialog(e) {
disableZoomButtons();
$.get('popUp.html', function(data) {
    modal.open({
        content : data
    });   
});
document.ontouchmove = function(e) {
    return false;
}
modal.open({
    content : "<div id='imgDiv'><img id='bombImg' src='img/bomb01.png'/><br>"
        + "</div>"
});
e.preventDefault();     
}     

请任何人帮助我解决这个问题。我在安卓4.0+中运行这个应用程序

你必须

onStart()函数放在doc ready之外:

function onStart ( touchEvent ) { 
        var flag = confirm("Are you sure want to defuse it?")
            if (flag == true) {                 
            $('#bombImg').attr('src', 'img/undefused.png');
        } else {   
            $('#bombImg').attr('src', 'img/bomb01.png');
        }
 touchEvent.preventDefault();
        modal.close();
  }
$(document).ready(function() {
$('body').append($overlay, $modal);
//Here tried with image id, div id and modal BUT No work
document.getElementById("content").addEventListener( 'touchstart',
function(e){ onStart(e); }, false );
});