javascript - 有没有办法防止用户交互暂停动画?

标签 javascript html animation

我有一个在网页上运行的动画。一旦我在触摸设备上与页面交互,动画就会停止。这不会发生在桌面上。我正在 iPad 上的 Chrome 和 Safari 上进行测试。

这是浏览器 vendor 有意做出的选择吗?有什么办法解决这个问题吗?

您可以转到此 JSFiddle page 进行测试在触摸设备上。

这是我正在使用的 JavaScript:

var offset = 0;
var requestAnimationFrame = window.requestAnimationFrame ||
                                              window.mozRequestAnimationFrame ||
                                              window.webkitRequestAnimationFrame ||
                                              window.msRequestAnimationFrame;

$('.box').on('click', function() {
  animateWithAnimationFrame();
});

function animateWithAnimationFrame() {
  if (offset === 500)
    return;
  offset++;
  $('.box').attr('style', '-webkit-transform: translate3d(' + offset + 'px, 0, 0)');
  requestAnimationFrame(animateWithAnimationFrame);   
}

我也尝试过使用 setTimeout 设置动画,但这没有什么区别。

谢谢。

最佳答案

参见 Android browser touch events stop display being updated inc. canvas/elements - How to work around?对于类似的问题。

实际上,仅关闭文档上的 touchstart(动画启动后)就可以了:http://jsfiddle.net/fBFkA/17/ :

function touchHandlerDummy(e) {
    return false;
}
document.addEventListener("touchstart", touchHandlerDummy, false);

关于javascript - 有没有办法防止用户交互暂停动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18962030/

相关文章:

javascript - 禁用按钮不会在 UpdatePanel 中提交回发

javascript - xpage/javascript 变量赋值中的奇怪行为

javascript - 如何使用单击事件切换元素中的文本

html - 滚动页面内容

c# - 使用 System.Windows.Media.Animation 移动按钮动画

css - onmouseenter KeyFrame 只工作一次

c# - 你能写 Javascript object init shorthand C# style 吗?

javascript - 允许表单发送 POST 请求但阻止页面重新加载 - Javascript

javascript - 使用嵌套表对齐问题的 TreeView

angularjs - ngAnimate 不在 ng-repeat 上添加动画类