How to remove/ignore :hover css style on touch devices
2020 Solution - CSS only - No Javascript
Use media hover with media pointer will help you resolve this issue. Tested on chrome Web and android mobile. I known this old question but I didn't find any solution like this.
@media (hover: hover) and (pointer: fine) {
a:hover { color: red; }
}
<a href="#" >Some Link</a>
How to disable :hover when scrolling on mobile?
You want to use pointer-events: none;
CSS:
.disable-hover {
pointer-events: none;
}
Javascript:
var body = document.body,
timer;
window.addEventListener('scroll', function() {
clearTimeout(timer);
if(!body.classList.contains('disable-hover')) {
body.classList.add('disable-hover')
}
timer = setTimeout(function(){
body.classList.remove('disable-hover')
},500);
}, false);
Reference article: http://www.thecssninja.com/css/pointer-events-60fps
How to prevent sticky hover effects for buttons on touch devices
Since this part of CSS Media Queries Level 4 has now been widely implemented since 2018, you can use this:
@media (hover: hover) {
button:hover {
background-color: blue;
}
}
Or in English: "If the browser supports proper/true/real/non-emulated hovering (e.g. has a mouse-like primary input device), then apply this style when button
s are hovered over."
For browsers that do not have this implemented (or didn't at the time of this original answer), I wrote a polyfill to deal with this. Using it, you can transform the above futuristic CSS into:
html.my-true-hover button:hover {
background-color: blue;
}
(A variation on the .no-touch
technique) And then using some client-side JavaScript from the same polyfill that detects support for hovering, you can toggle the presence of the my-true-hover
class accordingly:
$(document).on('mq4hsChange', function (e) {
$(document.documentElement).toggleClass('my-true-hover', e.trueHover);
});
Disable Hover on Mobile
Alright, you can't disable it, but you can not enable it.
@media only screen and (min-width: 768px) {
.desktopOnlyHover{
background-color:salmon;
}
}
Related Topics
Microsoft Cdn for Jquery or Google Cdn
How to Load the Web Page Content Based on User Scrolling
How to Remove Leading and Trailing White Spaces from a Given HTML String
JavaScript - Get Array Element Fulfilling a Condition
Typescript Compile to Single File
Clearing Localstorage in JavaScript
How to Convert from Hex to Ascii in JavaScript
How to Detect Chrome and Safari Browser (Webkit)
How to Make an Angularjs Directive to Stoppropagation
JavaScript Replace with Reference to Matched Group
How to Use Code from Script with Type=Module
Getting a Unhandledpromiserejectionwarning When Testing Using Mocha/Chai
When Should I Use a Semicolon After Curly Braces
Get Keys of JSON-Object in JavaScript
In Bootstrap Open Enlarge Image in Modal
ASP.NET MVC 3 Razor: Include JavaScript File in the Head Tag