简体   繁体   English

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

[英]:touch CSS pseudo-class or something similar?

I am trying to make a button, such that when the user clicks on it, it changes its style while the mouse button is being held down.我正在尝试制作一个按钮,这样当用户单击它时,它会在按住鼠标按钮的同时改变其样式。 I also want it to change its style in a similar way if it is touched in a mobile browser.如果在移动浏览器中触摸它,我也希望它以类似的方式改变其样式。 The seemingly-obvious thing to me was to use the CSS :active pseudo-class, but that didn't work.对我来说看似显而易见的事情是使用 CSS :active 伪类,但这不起作用。 I tried :focus, and it didn't work too.我尝试了:focus,但也没有用。 I tried :hover, and it seemed to work, but it kept the style after I took my finger off the button.我尝试了:悬停,它似乎工作,但是在我将手指从按钮上移开后它保持了这种风格。 All of these observations were on an iPhone 4 and a Droid 2.所有这些观察结果都是在 iPhone 4 和 Droid 2 上进行的。

Is there any way to replicate the effect on mobile browsers (iPhone, iPad, Android, and hopefully others)?有没有办法在移动浏览器(iPhone、iPad、Android 以及希望其他浏览器)上复制这种效果? For now, I am doing something like this:现在,我正在做这样的事情:

<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>

The :active pseudo-class is for desktop browsers, and the active class is for touch browsers. :active 伪类用于桌面浏览器,而 active 类用于触摸浏览器。

I am wondering if there is a simpler way to do it, without involving Javascript.我想知道是否有更简单的方法可以做到这一点,而不涉及 Javascript。

There is no such thing as :touch in the W3C specifications, http://www.w3.org/TR/CSS2/selector.html#pseudo-class-selectors W3C 规范中没有:touch之类的东西, http://www.w3.org/TR/CSS2/selector.html#pseudo-class-selectors

:active should work, I would think. :active应该可以工作,我想。

Order on the :active / :hover pseudo class is important for it to function correctly. :active / :hover伪类的顺序对于它正常运行很重要。

Here is a quote from that above link这是上面链接的引用

Interactive user agents sometimes change the rendering in response to user actions.交互式用户代理有时会更改渲染以响应用户操作。 CSS provides three pseudo-classes for common cases: CSS 为常见情况提供了三个伪类:

  • The :hover pseudo-class applies while the user designates an element (with some pointing device), but does not activate it. :hover 伪类在用户指定一个元素(使用一些指针设备)时应用,但不激活它。 For example, a visual user agent could apply this pseudo-class when the cursor (mouse pointer) hovers over a box generated by the element.例如,当光标(鼠标指针)悬停在元素生成的框上时,可视用户代理可以应用此伪类。 User agents not supporting interactive media do not have to support this pseudo-class.不支持交互式媒体的用户代理不必支持这个伪类。 Some conforming user agents supporting interactive media may not be able to support this pseudo-class (eg, a pen device).一些支持交互式媒体的符合标准的用户代理可能无法支持这个伪类(例如,笔设备)。
  • The :active pseudo-class applies while an element is being activated by the user. :active 伪类在用户激活元素时应用。 For example, between the times the user presses the mouse button and releases it.例如,在用户按下鼠标按钮并释放它的时间之间。
  • The :focus pseudo-class applies while an element has the focus (accepts keyboard events or other forms of text input). :focus 伪类在元素具有焦点时应用(接受键盘事件或其他形式的文本输入)。

Since mobile doesn't give hover feedback, I want, as a user, to see instant feedback when a link is tapped.由于移动设备不提供悬停反馈,因此作为用户,我希望在点击链接时看到即时反馈。 I noticed that -webkit-tap-highlight-color is the fastest to respond (subjective).我注意到-webkit-tap-highlight-color响应速度最快(主观)。

Add the following to your body and your links will have a tap effect.将以下内容添加到您的正文中,您的链接将具有点击效果。

body {
    -webkit-tap-highlight-color: #ccc;
}

I was having trouble with mobile touchscreen button styling.我在移动触摸屏按钮样式方面遇到了问题。 This will fix your hover-stick / active button problems.这将解决您的悬停棒/活动按钮问题。

 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>

The much upvoted comment by @gion_13 solved the issue for me: @gion_13 的好评为我解决了这个问题:

Add ontouchstart="" to your page's body element and the :active selector will work more as expected on touch screens.ontouchstart=""添加到页面的body元素中, :active选择器将在触摸屏上按预期工作。 Still not perfect in Chrome.在 Chrome 中仍然不完美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM