【问题标题】:Touch events are not working on Modal Popup触摸事件不适用于模态弹出窗口
【发布时间】:2023-03-22 07:25:02
【问题描述】:

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

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

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

下面是我用于 Modal Popup 的 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();     
}     

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

【问题讨论】:

    标签: javascript jquery html modal-popup


    【解决方案1】:

    您必须将 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 );
    }); 
    

    【讨论】:

    • 感谢您的回复。在准备好的功能之外尝试了但仍然无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 2015-11-09
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多