Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering the ":active" pseudo-class on a touchscreen

I'm currently developing a JavaScript application which will be deployed on a touchscreen kiosk running Chrome in kiosk mode.

I've noticed that when I use a mouse to click the buttons on the interface, the styles applied by the ":active" pseudo class is visible when the mouse is down. My problem is that the same button triggered by touching the screen does not trigger the active state.

Is there a way to make a touch event behave the same way as a mouse click?

like image 598
Nick Boyce Avatar asked Sep 30 '10 09:09

Nick Boyce


People also ask

Which pseudo-class activates once the user is focused on the element by using keyboard controls?

The :focus pseudo-class applies when an element is in a state that is ready to be interacted with, i.e. it has the focus of the input device.

What is doing active pseudo-class in CSS?

The :active CSS pseudo-class represents an element (such as a button) that is being activated by the user. When using a mouse, "activation" typically starts when the user presses down the primary mouse button.

How do you make links stay active when clicked?

A link becomes active when you click on it. Tip: The :active selector can be used on all elements, not only links. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :hover selector to style links when you mouse over them.


1 Answers

Assuming the CSS :active pseudo-class isn't working, you'll probably need to use DOM events.

Do the "mousedown" and "mouseup" events work with touchscreens? Assuming they do, you could try something like this:

addEventListener("mousedown", function (event) {
    if (event.target.setAttribute) {
        event.target.setAttribute("data-active", "");
    }
}, true);

addEventListener("mouseup", function (event) {
    if (event.target.removeAttribute) {
        event.target.removeAttribute("data-active");
    }
}, true);

Then within your CSS, replace :active with [data-active], like so:

div[data-active] {
    /* blah blah */
}

I don't think this will work quite the same... you may need to do some trickery to get child elements to work correctly, for instance.

like image 56
Pauan Avatar answered Sep 28 '22 00:09

Pauan