Managing touch devices and animations when hovering

Is there a way to remove or prevent all hover effects on a touch device? I've tried various scripts, but none seem to work for me. How can I test it with dev tools? I attempted this script but received an error: Cannot read property 'addEventListener' of null. Interestingly, it works here codepen. Could the error be because the dom tree has not loaded yet?

<html class="hover-active">

$(window).on('load', function() {

        if (!('addEventListener' in window)) {
            return;
        }

        var htmlElement = document.querySelector('html');

        function touchStart() {
            htmlElement.classList.remove('hover-active');

            htmlElement.removeEventListener('touchstart', touchStart);
        }

        htmlElement.addEventListener('touchstart', touchStart);
    });

I also tried this method:

if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){}

In addition, I experimented with using modernizr, which only seems to work for selectors inside html.touch and html.no-touch.

function is_touch_device() {
 return 'ontouchstart' in window        // works on most browsers 
  || navigator.maxTouchPoints;       // works on IE10/11 and Surface
};

if ( is_touch_device() ) {
  $('html').addClass('touch')
} else {
  $('html').addClass('no-touch')
} 


html.touch .hover-me:hover {
   pointer-events: none !important;
   cursor: pointer;

}
html.touch a:hover {
   pointer-events: none !important;
   cursor: pointer;
}

/* FOR THE DESKTOP, SET THE HOVER STATE */
html.no-touch .hover-me:hover {
   width: auto;
   color:blue;
   background:green;
}
html.no-touch a:hover {
   width: auto;
   color:blue;
   background:green;
}

However, my goal is to remove all :hover selectors. The * selector does not work for this purpose. It seems like I'm still struggling to make it work.

  .hover-me:hover {
     background: yellow;
 }
html.touch .hover-me:hover {
   pointer-events: none !important;
   cursor: pointer;

}

Answer №1

If you want to eliminate all hover effects on touch screen devices, one approach is to utilize CSS Media Queries to ensure that the device has full support for hover actions before implementing any :hover styles. This can be achieved by encapsulating the styles within @media (hover: hover).

@media (hover: hover){
  .hover-me:hover {
     background: yellow;
 }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extract latitude and longitude coordinates from a dataset by applying a rectangular filter

I have developed a program that extracts information from a JSON object and showcases it on a webpage, specifically focusing on Public Bike Stations. The JSON object includes the latitude and longitude of each station, making it easy to locate them. My cu ...

Dropdown search menus are failing to appear in the correct location

I have 4 dependent search dropdown menus side by side. There are two issues I am facing: Whenever I type in any of the drop-down menus, the MySQL-connected lists appear but not directly beneath the actual 'input-type-search-box'. Additional ...

Show the information received from ajax on the webpage

I am facing an issue with displaying the value I receive from AJAX data in this modal. I have set the id="view_name" on a span element but for some reason, it's not showing up. Below is the code snippet that triggers the fun_view function: <td> ...

Top method for identifying "abandoned words" within text that has been wrapped

Our content and design teams have a specific request to prevent paragraphs from ending with an "orphan word" - a single word on the last line of text that has wrapped onto multiple lines. The designer's proposed solution is to adjust the margins sligh ...

The AngularJs project is currently experiencing issues when trying to function as a single page web application

I'm currently working on incorporating AngularJS into an existing web project that uses jQuery. Here is a snippet of my code: <div class="medya" ng-repeat="d in data" masonry-item-dir> <a href="{{d.Url}}" class="onizleme"><img src="{ ...

Why isn't the image showing up as a list style?

I can't seem to get the image to show up on my page. Can anyone help me figure out what's wrong? ul li { background-image: url(logo.png); background-repeat: no-repeat; padding-left: 0px; } This is what I'm currently seeing: This is what ...

Changing or toggling the visibility of links once the week is finished

I have a total of 4 links available, but I want to display only one link at a time, highlighting a different lunch option every week. Once all 4 weeks have passed, the first lunch menu will be shown again, starting from Monday. <div class="wrapper& ...

Guide to putting a new track at the start of a jPlayer playlist

I am currently working on a website that utilizes the jPlayer playlist feature. I am facing an issue, as I need to implement a function that adds a song to the beginning of the playlist, but the existing add function only appends songs to the end of the pl ...

Smooth vertical scrolling for the mousewheel using jQuery

I am currently working on a parallax website and I want to enhance the user experience by making the page scroll more smoothly with the mousewheel. I found inspiration from this website: I would love to incorporate a similar smooth vertical scrolling and ...

There was a lack of information received from the callback function when utilizing jQuery AJAX with JSON in PHP

I've been experimenting with the code found here: https://gist.github.com/Exeu/4674423. This particular code is designed to connect to Amazon's wsdl and fetch product information. Unfortunately, I've encountered some difficulties in getting ...

Using the jQuery datetimepicker in the 12-hour format with AM and PM

Hey there! I'm looking to enhance my current code by adding a feature that displays time in a 12-hour format with AM/PM. You can check out an example of this functionality on jsfiddle by following this link: http://jsfiddle.net/8ztsjcos/3/. Thank you ...

"Uncovering the mystery of the missing element in Jquery's open

I have developed a click-to-show feature that opens and closes a div from left to right. You can view the demo here on jsfiddle.net In the demo, you will find a green-colored div. Upon clicking this div, another div opens from left to right. An interesti ...

Angular directive for concealing the 'ancestor' of an element

I have created a code snippet that effectively hides the 'grandparent' element of any '404' images on my webpage. Here is the code: function hideGrandparent(image){ image.parentNode.parentNode.style.display = 'none'; } < ...

Is it possible for CSS div to have a height of 100% when placed within fluid parents

When the parent div's height is set to 100%, you can also set the child to 100% and it will function correctly. However, if the parent's height is determined by its content, the height attribute does not work as expected. Are there any effective ...

What is the method for obtaining the entire object as a response following a click?

I am working on a basic JavaScript snippet: var image = [{name: "Breakfast", age: 100, author: "Alice"},{name: "Dinner", age: 10, author: "Teddy"}]; function gallery(name, content) { this.name = name; this.c ...

What is the best way to design a shape (a quadrilateral with rounded edges) using CSS?

Struggling with creating a widget in React Js that has an image as the background? The image is designed to be dynamic, changing color and size based on the device. I've attempted using inline SVG and SVG within an img tag but can't seem to contr ...

Application utilizing Meteor to connect with external websites or applications

Hey everyone, I'm in the process of developing an application that features a directory of stores. One key requirement is that each store has a unique view created with either Flash or JavaScript. The special view components have already been develope ...

Utilizing HTML and JavaScript to dynamically switch stylesheets based on the width of

When it comes to using stylesheets for mobile and non-mobile devices, there is a need for different approaches. The idea of comparing browser height and width in JavaScript seems like a good one, but the implementation may not always work as expected. ...

Updating children of specified parents using jQuery

I have a collection of files in a directory and some file icons need to be changed as the default ones are not specific enough. Due to limitations, I can only achieve this using jQuery to insert the code since the mimetype function cannot be modified. Bel ...

Trouble with loading Google chart data from a local JSON file

The Issue Running multiple charts on a site simultaneously is causing page blocking while loading all the data before rendering the charts. The goal is to trigger the render function for each chart as soon as the data is available, rather than waiting for ...