【问题标题】:Controlling events on Apple Ipad's Safari控制 Apple Ipad 的 Safari 上的事件
【发布时间】:2012-04-02 17:39:11
【问题描述】:

嗯,这将是一个艰难的过程。我想我已经用尽了所有的选择,让我们看看你能不能想出更好的办法。

我有一个水平旋转木马,正在使用touchstarttouchmovetouchend 来控制它。就本例而言,html 结构类似于:

<div>
   <ul id="strip">
     <li><a>...</a></li>
     <li><a>...</a></li>
     ...................
   </ul>
</div>

我已经分离了我的事件处理程序,以使鼠标与触摸事件的行为有所不同,因此,只考虑触摸,我有:

var strip = document.getElementById('strip');
strip.addEventListener('touchstart', touchStartHandler);
document.addEventListener('touchmove', touchMoveHandler);
document.addEventListener('touchend', touchEndHandler);

我希望即使用户的手指在我的条带之外也能进行水平滚动,因此我将touchmovetouchend 事件附加到文档中。

起初,我认为当用户滚动我的轮播时,让浏览器保持不动是很自然的,所以我的touchMoveHandler 看起来像:

function touchMoveHandler(evt){
   evt.preventDefault();
   ...................
}

...这样,当用户的手指在 Y 轴上的位置变化时,浏览器不会垂直平移。我们的可用性专家不这么认为,我现在实际上同意他的观点。他希望浏览器能够正常响应,除非手指的移动完全或接近完全水平。

无论如何,这可能是太多的信息,所以我现在要详细说明我的实际问题。这是我正在开发的原型的 sn-p,作为概念证明:

var stopY, lastTime, newTime;

var touchStartHandler(evt){
   ...
   stopY = true;
   lastTime = new Date();
   ...
};
var touchMoveHandler(evt){
   ...
   //this following code works like a setInterval, it turns stopY on and off every 1/2 sec
   //for some reason, setInterval doesn't play well inside a touchmove event
   newTime = new Date();
   if(newTime - lastTime >= 500){
      stopY = !stopY;
      lastTime = newTime;
   }
   ...
   if(stopY){
      evt.preventDefault();
   }
   ...
}

我绝对确定这段代码是原始的,我使用控制台日志对其进行了调试,除了计算浏览器通过stopY 变量平移之外,一切都在做它应该做的事情。

如果我运行以stopY = true 开头的代码,则没有浏览器平移,如果我以stopY = false 开头,浏览器将按预期平移。问题是我希望这种行为每半秒改变一次,但事实并非如此。

我希望我没有让你把这件事复杂化,但这真的很具体。

更新:

您可以尝试以下链接(在 Ipad 或 Iphone 上):
http://andrepadez.com/ipadtouch
http://andrepadez.com/ipadtouch?stopy=false

使用查看源代码,查看整个代码

【问题讨论】:

    标签: javascript ipad mobile dom-events mobile-safari


    【解决方案1】:

    你能试试stopY = !stopY;吗?

    更新

    一旦您执行preventDefault(),滚动将无法返回,直到touchend 触发或您启用它。以下内容可能会为您提供您正在寻找的行为:

    if(stopY){ return false; }
    else { return true; }
    

    或者为了简单起见只是return !stopY;...

    【讨论】:

    • 对不起,我要编辑它,我从我的代码中复制/粘贴,我正在处理一个对象范围
    • @AndréAlçadaPadez,我已经更新了我的答案。您可能希望将计时器调高至 2000 以检查 iPad 的行为。希望这能解决您的问题!
    猜你喜欢
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    相关资源
    最近更新 更多