【问题标题】:JS distinguish click and scroll on iPhoneJS区分iPhone上的点击和滚动
【发布时间】:2018-01-30 08:16:18
【问题描述】:

我正在尝试关闭 Sidemenu onclick。但是,touchstart 也将滚动检测为单击,touchend 也是如此。 如何只检测 iPhone 上的点击(不是滚动)?

  $('#html').on("click touchstart",function(e) {
      var optionsmenue = $(".adminmenu_label");
      if(!optionsmenue.is(e.target) && optionsmenue.has(e.target).length === 0) {
        document.getElementById("Optionsmenu").style.width = "0%";
        document.getElementById("Optionsmenu").style.transition = "0.2s ease-out";
        document.getElementById("adblue").style.display = "block";
        document.getElementById("whatever").style.display = "block";  
        document.getElementById("not_related").style.display = "block";
        document.getElementById("still_not_related").style.display = "block"; 
        document.getElementById("still_still_not_related").style.width = "100%"; 
      }
    });

【问题讨论】:

标签: javascript jquery ios


【解决方案1】:

检测 iOS 并添加 cursor:pointer 对我有用,IOS 似乎有事件委托问题。

var iOS = ["iPad","iPhone","iPod"].indexOf(navigator.userAgent) > -1;

if(iOS) {
   $('body').css({ cursor : 'pointer' });
}

$('#html').on("click",function(e) {
    // No need for touch start click will do the trick;
});

【讨论】:

  • @tipsfedora 欢迎您,尽管用户代理检测并不是最好的解决方案,但我敦促您在空闲时间找到更好的解决方案 :)
  • 我应该建议你用更简单的解决方案替换正则表达式,比如 ["one", "two", "three"].indexOf("one") >= 0 (记住等号!)
  • 我的想法是正则表达式是重型引擎,indexOf 是更节省资源的解决方案。
  • 好的,这样不行。试试这个: ["iPad", "iPhone" ,"iPod"].indexOf(navigator.platform) > -1; |-| 10 倍到stackoverflow.com/q/19877924/849516
【解决方案2】:

应该删除“touchstart”

$('#html').on("click touchstart",function(e) {

【讨论】:

  • iPhone 没有检测到“点击”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多