javascript - Mobile WebKit 记住点击位置?

标签 javascript jquery iphone css mobile-webkit

我目前正在开发一个用于 WebKit 设备的私有(private)框架。我创建了一系列 ListView ,当用户触摸开始时,我正在向这些 View 应用 hover 类。这是我用来添加它的 jQuery/JavaScript 代码,以及匹配类的 CSS:

    $('*').bind('touchstart', function() { $(this).addClass('hover'); }).bind('touchend', function() { $(this).removeClass('hover') });

还有 CSS:

 ul li:hover,
 ul li.hover {
     background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(5,140,245,1)), color-stop(100%,rgba(1,96,230,1)));
     background: -webkit-linear-gradient(top, rgba(5,140,245,1) 0%,rgba(1,96,230,1) 100%);
     color: #fff
 }       
     ul li:hover a,
     ul li.hover a {
         background-position: right -38px
     }

这似乎在 Chrome(或某些支持 Webkit 的桌面浏览器)中查看时按预期工作,但如果鼠标位置在屏幕转换之间不移动,则新屏幕上的列表元素会悬停.这显然是意料之中的,因为 :hover 伪类已被触发。

但是,我没想到在 iPhone 的 Mobile Safari 中会发生同样的行为。以下是一些屏幕截图和场景的简要说明。我在 List Views 元素(它是 li 中的 a 元素)上点击一次,然后将手指从屏幕上移开。显示一个包含另一个列表的新 div。没有在现在显示的新 div 上点击任何东西,第二个 li 有一个 hover 类,即使我没有触及它...

enter image description here enter image description here

谁能帮我调试这个,或者弄清楚为什么会在 Mobile WebKit 上发生这种情况?这很烦人,因为它的 HCI 很差。我尝试添加以下行来纠正问题,但没有成功:

if(window.location.hash)
{
    var the_hash = window.location.hash.substring(1);
    if($('#' + the_hash).length > 0)
    {
        $('.current').removeClass('current');
        $('#' + the_hash).find('*').removeClass('hover');
        $('#' + the_hash).addClass('current');
    }
}
else
{
    $('div').eq(0).addClass('current');
} 

非常感谢您的帮助,抱歉啰嗦!

最佳答案

试试这个

$('body').bind('touchend',function(){
     $('#p-promotion-main .e-container.hover').removeClass('hover');
});

关于javascript - Mobile WebKit 记住点击位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7189245/

相关文章:

jquery - Select2 文本框宽度异常

带有附件 View 的 iOS 8 自调整单元格

iphone - uiwebview 没有像在 safari 中那样正确显示 PDF

iphone - 如何使用注释降低 MKMapview 的缩放级别?

c# - 使用 JavaScript/jQuery 重定向到 ASP.NET MVC 中的另一个页面

javascript - MVC Controller 方法不被 View 中的 jquery 触发

javascript - Nuxt JS 事件监听器触发两次

php - php 代码中的 javascript

javascript - 在不使用 <input type ="text"> 的情况下创建输入文本框

javascript - 在对象内的函数之间传递值