【问题标题】:Detect mousedown and touchstart pageX on another function在另一个函数上检测 mousedown 和 touchstart pageX
【发布时间】:2017-08-11 14:26:00
【问题描述】:

我有一个事件来检测鼠标或触摸并使用 setTimeout 将此事件发送到另一个函数。

我检测鼠标或触摸事件没有问题,我不能做的是向函数发送触摸事件。

$(divst + ' #sortable1').on('touchstart mousedown',function(e){
    e.preventDefault();
        setTimeout(function(){
                    StarTrans(e); // <- sending event
        },200);
});
function StarTrans(e){
    if(e.pageX==='undefined'){ // this does not work
        $(dtarget).offset({left:e.changedTouches[0].pageX-33,top:e.changedTouches[0].pageY-58});
        $('#titring').html(e.changedTouches[0].pageX + ',' + e.changedTouches[0].pageY);
    }else{ // this works
        $(dtarget).offset({left:e.pageX-33,top:e.pageY-58});
        $('#titring').html(e.pageX + ',' + e.pageY);
    }
}

mousedown 按预期工作,touchstart 返回“未定义”。

我尝试了这些事件来获取 pageX

  • e.changedTouches[0].pageX
  • e.targetTouches[0].pageX
  • e.originalEvent.touches[0].pageX
  • e.touches[0].pageX
  • e.pageX

谢谢!

【问题讨论】:

    标签: javascript settimeout touch-event jquery-events touchstart


    【解决方案1】:

    首先, if(e.pageX==='undefined'){ 不正确。如果你需要那个条件,更好的if (typeof e.pageX == 'undefined') {,更好的if (e.type == 'touchstart') {

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2015-12-27
      相关资源
      最近更新 更多