:touch CSS 伪类或类似的东西?

IT技术 javascript jquery html css mobile
2021-02-24 10:50:44

我正在尝试制作一个按钮,这样当用户单击它时,它会在按住鼠标按钮的同时改变其样式。如果在移动浏览器中触摸它,我还希望它以类似的方式更改其样式。对我来说看似显而易见的事情是使用 CSS :active 伪类,但这不起作用。我试过:focus,但也没有用。我试过 :hover ,它似乎有效,但在我将手指从按钮上移开后它保持了样式。所有这些观察都是在 iPhone 4 和 Droid 2 上进行的。

有什么方法可以在移动浏览器(iPhone、iPad、Android 以及其他)上复制这种效果?现在,我正在做这样的事情:

<style type="text/css">
    #testButton {
        background: #dddddd;
    }
    #testButton:active, #testButton.active {
        background: #aaaaaa;
    }
</style>

...

<button type="button" id="testButton">test</button>

...

<script type='text/javascript' src='http://code.jquery.com/jquery-1.6.1.min.js'></script>
<script type='text/javascript'>
    $("*").live("touchstart", function() {
      $(this).addClass("active");
    }).live("touchend", function() {
      $(this).removeClass("active");
    });
</script>

:active 伪类用于桌面浏览器,而 active 类用于触摸浏览器。

我想知道是否有更简单的方法来做到这一点,而不涉及 Javascript。

3个回答

:touchW3C 规范中没有这样的东西http://www.w3.org/TR/CSS2/selector.html#pseudo-class-selectors

:active 应该工作,我想。

:active/:hover伪类的顺序对于它正确运行很重要。

这是上面链接的引述

交互式用户代理有时会根据用户操作更改渲染。CSS 为常见情况提供了三个伪类:

  • :hover 伪类适用于用户指定元素(使用某些指针设备)但不激活它的情况。例如,当光标(鼠标指针)悬停在元素生成的框上时,可视化用户代理可以应用这个伪类。不支持交互式媒体的用户代理不必支持这个伪类。一些支持交互式媒体的符合要求的用户代理可能无法支持这个伪类(例如,笔设备)。
  • :active 伪类在用户激活元素时适用。例如,在用户按下鼠标按钮和释放它之间的时间。
  • :focus 伪类在元素具有焦点时适用(接受键盘事件或其他形式的文本输入)。
@mikez302:使用:active伪类。它应该工作,但在一些浏览器,你需要一个小的黑客攻击,使其工作:附加一个事件处理程序touchstart对身体活动(例如:<body ontouchstart="">
2021-04-16 10:50:44
只是在最后一个答案中触及@mikez302 评论,他们是 HTML5 移动样板中的一个巧妙修复,链接到本文 - alxgbsn.co.uk/2011/10/17/...
2021-04-18 10:50:44
@mikez302 - 我想你知道我想引用我的来源。我同意你的看法,除了我认为订单非常重要的观点之外,没有任何理由认为它不起作用。我确实看到有人发布了一些关于 IOS 和 Android 的 css 伪选择器的内容,但我无法谈论这些技术。
2021-05-07 10:50:44
我在示例中切换了 :active 和 :hover 规则,但它似乎没有做任何事情。
2021-05-14 10:50:44
我知道没有像 :touch 这样的东西。我想知道是否有类似的东西。:active 似乎合理,但在我的测试中不起作用。
2021-05-15 10:50:44

由于移动设备不提供悬停反馈,因此作为用户,我希望在点击链接时看到即时反馈。我注意到这-webkit-tap-highlight-color是最快的react(主观)。

将以下内容添加到您的正文中,您的链接将具有点击效果。

body {
    -webkit-tap-highlight-color: #ccc;
}
@Adam 是的,我记得我无法解决这个问题,但这只是几分之一秒对我来说并不是什么大不了的事,尤其是它会让用户获得他点击的反馈(这是在这段时间里对他们来说什么是重要的)
2021-04-20 10:50:44
我实际上通过将一个函数绑定到 document.ontouchstart 事件(通过 jQuery),并使用一个空回调,最终使 :active 伪样式工作。
2021-04-28 10:50:44
不错的主意:-) 为此,我完全避免使用 javascript。
2021-04-29 10:50:44
这似乎是最可靠的,但它看起来并不是最好的。背景颜色似乎延伸到元素之外,使其在瞬间看起来更大。
2021-05-06 10:50:44
只是分享这-webkit-tap-highlight-color是一个非标准的 CSS 功能,我正在使用:active它,它真的很合适并且很有意义。
2021-05-14 10:50:44

我在移动触摸屏按钮样式方面遇到了麻烦。这将解决您的悬停杆/活动按钮问题。

body, html {
  width: 600px;
}
p {
  font-size: 20px;
}

button {
  border: none;
  width: 200px;
  height: 60px;
  border-radius: 30px;
  background: #00aeff;
  font-size: 20px;
}

button:active {
  background: black;
  color: white;
}

.delayed {
  transition: all 0.2s;
  transition-delay: 300ms;
}

.delayed:active {
  transition: none;
}
<h1>Sticky styles for better touch screen buttons!</h1>

<button>Normal button</button>

<button class="delayed"><a href="https://www.google.com"/>Delayed style</a></button>

<p>The CSS :active psuedo style is displayed between the time when a user touches down (when finger contacts screen) on a element to the time when the touch up (when finger leaves the screen) occures.   With a typical touch-screen tap interaction, the time of which the :active psuedo style is displayed can be very small resulting in the :active state not showing or being missed by the user entirely.  This can cause issues with users not undertanding if their button presses have actually reigstered or not.</p>

<p>Having the the :active styling stick around for a few hundred more milliseconds after touch up would would improve user understanding when they have interacted with a button.</p>