Home > Article > Web Front-end > How to prevent sticky hover effect for buttons on touch devices?
As you know, touch screen technology does not support :hover behavior. therefore, When creating a responsive website, you should carefully consider when and where to use :hover interaction. Some touch screen devices will lose the hover effect of simple links Open a URL. You will see the :hover style for a short period of time before the page changes On iOS, because :hover is activated before the click event
These are minor issues that have no impact on the functionality of the site. Here is a:hover, which Either use the display or visibility CSS properties to show or hide another element, yes The real problem.
There are two methods that can be used to solve this problem.
Devices without JavaScript - You can use CSS media query functions to fix it. Devices that support this feature hover are referred to by the condition "hover: hover". Adding the following CSS only on such devices Using media queries along with this condition is guaranteed.
@media(hover: hover) { #btn:hover { background-color: #ccf6c8; } }The Chinese translation of
This only adds a hover effect for devices that support hover; there is no hover effect for touch devices. exist In this case, the button's background color changes when the mouse is hovered over it. On touch devices, there is There is no hover effect, so the button remains in its original state.
<!DOCTYPE html> <html> <title>How to prevent sticky hover effects for buttons on touch devices - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> #myButton { background-color: #096381; margin: 3%; font-size: 30px; color: #fff; } @media (hover: hover) { #myButton:hover { /*On devices that support hover, add a hover effect to the button.*/ background-color: #0a92bf; } } </style> </head> <body> <h2>Welcome to TutorialsPoint!</h2> <p>Our mission is to deliver Simply Easy Learning with clear, crisp, and to-the-point content on a wide range of technical and non-technical subjects without any preconditions and impediments.</p> <button type="button" id="myButton"> Submit </button> </body> </html>
The code above will produce the following output: This is the result on a non-touch screen.
Second step using JavaScript - In this method the following JavaScript function will be used to check Whether we use touch devices or not. Whenever the user touches an element, The ontouchstart event response returns a true value. Maximum number of consecutive touch points that the device supports is returned by navigator.maxTouchPoints.
The features supported by this device are returned by navigator.maxTouchPointsIn navigator.msMaxTouchPoints, the same functionality is available under the vendor prefix "ms" Targets IE 10 and earlier browsers. Therefore, if the device supports touch functionality, the specified function returns true.
function is_touch_enabled() { return ('ontouchstart' in window) || (navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0); }
In the code below, this class provides a hover effect for the button in CSS −
<!DOCTYPE html> <html> <title>How to prevent sticky hover effects for buttons on touch devices - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> #myButton { background-color: #096381; margin: 3%; font-size: 30px; color: #fff; } .myButton2:hover { background-color: #0a92bf !important; /*The myButton2 class now has a hover effect.*/ } </style> </head> <body onload="touchHover()"> <p>TutorialsPoint have established a Digital Content Marketplace to sell Video Courses and eBooks at a very nominal cost. <br>You will have to register with us to avail these premium services.</p> <button type="button" id="myButton">Submit</button> <script> function touchHover() { function is_touch_enabled() { // Verify that touch is turned on return "ontouchstart" in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0; } if (!is_touch_enabled()) { // Add the "myButton2" class if touch is not enabled. let verifyTouch = document.getElementById("myButton"); verifyTouch.classList.add("myButton2"); } } </script> </body> </html>
The above code will produce the following output: This is the result on a non-touch device.
Since there is no hover effect on touch devices, the buttons remain in their original state.
The above is the detailed content of How to prevent sticky hover effect for buttons on touch devices?. For more information, please follow other related articles on the PHP Chinese website!