Home > Web Front-end > JS Tutorial > How to Replicate Hover Effects for Long Touches in Touch-Enabled Browsers?

How to Replicate Hover Effects for Long Touches in Touch-Enabled Browsers?

Linda Hamilton
Release: 2024-10-22 19:04:02
Original
792 people have browsed it

How to Replicate Hover Effects for Long Touches in Touch-Enabled Browsers?

Simulating Hover Effects with Touch Gestures

Scenario:

You have a webpage with HTML elements styled using CSS. Specifically, you have elements with a "hover" effect, where their color changes when the cursor hovers over them. However, in a touch-enabled browser, you want to replicate this behavior for long touches.

Solution:

To achieve this, follow these steps:

  1. Modify CSS: Add the following class to your HTML elements for which you want to enable hover effects:
<code class="css">p {
  color:black;
}

p:hover, p.hover_effect {
  color:red;
}</code>
Copy after login

This ensures that the "hover_effect" class replicates the styling of the ":hover" pseudo-class.

  1. Add JavaScript: Implement the following code using jQuery:
<code class="javascript">$(document).ready(function() {
    $('.hover').on('touchstart touchend', function(e) {
        e.preventDefault();
        $(this).toggleClass('hover_effect');
    });
});</code>
Copy after login
  1. Prevent Default Browser Behaviors: To prevent the browser's default touch actions (e.g., context menu), add the following CSS to your style sheet:
<code class="css">.hover {
-webkit-user-select: none;
-webkit-touch-callout: none;        
}</code>
Copy after login

This combination of modifications enables touch-and-hold gestures on eligible elements to simulate hover events for touch-enabled browsers.

The above is the detailed content of How to Replicate Hover Effects for Long Touches in Touch-Enabled Browsers?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template