Home > Web Front-end > JS Tutorial > How to prevent sticky hover effect for buttons on touch devices?

How to prevent sticky hover effect for buttons on touch devices?

WBOY
Release: 2023-08-22 12:25:06
forward
887 people have browsed it

How to prevent sticky hover effect for buttons on touch devices?

On touch devices, when adding a hover effect using CSS, the element becomes fixed. This article will teach you Tell us how to solve this problem. On touch devices, there is no hover effect, so the button remains in its original state. No Using JavaScript: You can use the media query feature of CSS to solve this problem. Supported devices hover are those that match the requirement "hover: hover". To ensure the following CSS is added On these devices only, use media queries with this condition. Only devices that support hover will No hover effects are visible on touch devices. When you hover over this, you can see the hover effect added
  • 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.

code segment

@media(hover: hover) {
   #btn:hover {
      background-color: #ccf6c8;
   }
}
Copy after login
The Chinese translation of

Example 1

is:

Example 1

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>
Copy after login

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

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

code segment

function is_touch_enabled() {
   return ('ontouchstart' in window) ||
   (navigator.maxTouchPoints > 0) ||
   (navigator.msMaxTouchPoints > 0);
}
Copy after login

Example 2

is translated as:

Example 2

In this example, let's see how to add a class to our button if touch functionality is not enabled. As follows:

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template