【问题标题】:clearInterval() doesnt work on iPadclearInterval() 在 iPad 上不起作用
【发布时间】:2013-09-26 11:00:01
【问题描述】:

这就是我的工作:

var scrolling;

$('#nw_scroll_down').on('mousedown', function(){
  scrolling = setInterval(function() {
      $('.mod_article').scrollTop( $('.mod_article').scrollTop() + 5 );
    },25);    
});

$('#nw_scroll_down').on('mouseup', function(){
  if (scrolling) {
    window.clearInterval(scrolling);
    scrolling = false;
  }
});

在 $(document).ready(function(){}); 内;

一切正常,直到上线

window.clearInterval(scrolling);

这在 PC 上运行良好,但在 iPad 上却不行。谁能想象它为什么不能在 ipad(chrome 浏览器)上运行?

【问题讨论】:

  • 您是否检查过scrolling 变量等于true? iOS 支持mousedown/mouseup 还是有替代方案?你有没有做过任何调试?
  • 如果您在 ipad 中单击更长的图片会弹出一个菜单来保存图片和内容...所以滚动条的设计方式无法正常工作在 ipad 中?
  • 调试是的,如果我注释掉 //window.clearI... 行并做一些 alert('stuff');我得到消息。所以这个变量是真的。不知道是否支持mousedown/mouseup,但我想不会像在PC上那样......该死

标签: javascript jquery ipad


【解决方案1】:

您应该使用touchstarttouchend iPad 事件而不是mousedown,如Apple documentationMozilla documentation 中所述。

试试这个:

$('#nw_scroll_down').bind( "touchstart", function(e){
      scrolling = setInterval(function() {
      $('.mod_article').scrollTop( $('.mod_article').scrollTop() + 5 );
    },25); 
});

$('#nw_scroll_down').bind('touchend', function(){
  if (scrolling) {
    window.clearInterval(scrolling);
    scrolling = false;
  }
});

为了让它在桌面和触控设备上都能正常工作,你可以试试这个:

$('#nw_scroll_down').on("mousedown touchstart", function(e){

.

更新


另一个解决方案given by Apple documentation 似乎是将内联事件onclick="void(0)" 添加到您拥有mousedown 事件的元素中。

可点击元素是链接、表单元素、图像地图区域或任何其他带有 mousemove、mousedown、mouseup 或 onclick 处理程序的元素。可滚动元素是具有适当溢出样式、文本区域和可滚动 iframe 元素的任何元素。由于这些差异,您可能需要将一些元素更改为可点击元素,如“Making Elements Clickable,” 中所述,以在 iPhone OS 中获得所需的行为。

【讨论】:

  • 是的,这对我有用,尽管我认为 onclick="void(0)" 没有任何用处。这是否应该防止弹出保存图片的上下文菜单?还有其他已知的预防方法吗?
  • html { -webkit-user-select: none; -webkit-touch-callout:无; } 这对我有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 2011-03-15
相关资源
最近更新 更多