jQuery Click Mobile on Mobile

时间:2013-09-26 16:56:05

标签: javascript jquery css mobile

我在Mobile上遇到了JQuery点击事件的问题。我搜索了SO的答案,并发现有关在Safari / Chrome等移动浏览器中使用点击事件的建议存在冲突。

以下是我的笔,它是一个更大项目的片段。为了这篇文章的目的,这是我在5分钟内聚集在一起的东西。

执行JS并在桌面浏览器上切换CSS类,但在移动设备上根本不切换。我错过了什么?

http://codepen.io/anon/pen/FknId

谢谢!

HTML

<a href="#" class="btn-navbar-responsive">Menu</a>
<ul class="navbar-menu navbar-hidden">
    <li><a href="#">Link 1</a></li>
    <li><a href="#">Link 1</a></li>
    <li><a href="#">Link 1</a></li>
</ul>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

JS

$(".btn-navbar-responsive").click(function() {
    $(".navbar-menu").toggleClass("navbar-hidden navbar-visible");
});

CSS

.btn-navbar-responsive {
    padding: 15px 20px;
    color: white;
    text-decoration: none;
    font-size: 16px;
    font-family: "Helvetica";
    background-color: #50D78B;
 }


.navbar-visible {
    -webkit-transition: opacity, ease-in, 350ms;
    -moz-transition: opacity, ease-in, 350ms;
    -ms-transition: opacity, ease-in, 350ms;
    transition: opacity, ease-in, 350ms;
    -webkit-transition: top, ease-in, 350ms;
    -moz-transition: top, ease-in, 350ms;
    ms-transition: top, ease-in, 350ms;
    transition: top, ease-in, 350ms;
    top: 10px;
    opacity: 1;
    position: relative;
    z-index: 999;
}

.navbar-hidden {
    -webkit-transition: opacity, ease-in, 350ms;
    -moz-transition: opacity, ease-in, 350ms;
    -ms-transition: opacity, ease-in, 350ms;
    transition: opacity, ease-in, 350ms;
    -webkit-transition: top, ease-in, 350ms;
    -moz-transition: top, ease-in, 350ms;
    -ms-transition: top, ease-in, 350ms;
    transition: top, ease-in, 350ms;
    top: 0px;
    opacity: 0;
    position: relative;
    z-index: 999;
}

3 个答案:

答案 0 :(得分:0)

您可以尝试使用此类触摸事件检测事件委派 //是否支持检测触摸

var event= ('ontouchstart' in document.documentElement) ? "touchstart" : "click";

//委派适当的事件

$(".btn-navbar-responsive").on(event, function() {
    $(".navbar-menu").toggleClass("navbar-hidden navbar-visible");
});

答案 1 :(得分:0)

一个简单的解决方法是添加CSS属性:

cursor: pointer;

答案 2 :(得分:-1)

尝试.on()而不是click()事件。

http://api.jquery.com/on/

$(".btn-navbar-responsive").on('click', function() {
    $(".navbar-menu").toggleClass("navbar-hidden navbar-visible");
});