What is the reason that when a <div> click on a mobile device includes a jQuery ('#item').fadeOut() function, the CSS of the '#item' element maintains its previous hover state

While creating a mock back to top button, I encountered an issue with my jQuery call to fadeOut() behaving differently on mobile devices (this discrepancy can be observed using any desktop browser simulator).

You can find the source code here: https://codepen.io/ehouarn-perret/pen/ZRgJXE

The setup involves the following code snippet (utilized in the jQuery shorthand when the document is ready):

window.setupBackToTopButton = function () {

    function updateBackToTopButton() {
        if ($(window).scrollTop() > 50) { 
            $('#back-to-top-button').fadeIn();
        } else { 
            $('#back-to-top-button').fadeOut();
            $('#back-to-top-button').blur();
        } 
    }

    $(window).on("scroll touchmove", updateBackToTopButton); 

    updateBackToTopButton();

    $('#back-to-top-button').click(function(){ 
        $("html, body").animate({ scrollTop: 0 }, 500); 
        return false; 
    }); 
};

The button comes with the following CSS:

#back-to-top-button {
    position: fixed;
    right: 10px;
    bottom: 10px;
    cursor: pointer;
    padding: 7px 12px 7px 12px;
    background-color: #cc0c0c;
    opacity: 0.5;
    display: none;
    z-index: 9999;
}

#back-to-top-button:hover {
    -o-transition: 300ms;
    -ms-transition: 300ms;
    -moz-transition: 300ms;
    -webkit-transition: 300ms;
    transition: 300ms;
    background-color: #9c0909;
    opacity: 1;
}

The issue arises specifically on mobile/tablet versions where the hover state persists (background-color: #9c0909;, opacity: 1).

Any insights on why this behavior occurs?

Solution:

As explained in the accepted answer, this behavior relates to the sticky nature of touch devices. After reviewing this article I opted to incorporate the touchstart event alongside the click.

Replacing this:

$('#back-to-top-button').click(function(){ 
    $("html, body").animate({ scrollTop: 0 }, 500); 
    return false; 
}); 

With this:

$('#back-to-top-button').on('click touchstart', onBackToTopButtonClick);

This adjustment sort of makes sense as the hovering state on a touch device leads to stickiness.

Answer №1

The reason for this issue is the sticky hover behavior on mobile devices, which is a well-known bug or behavior. Once you touch anywhere else, the :hover state will disappear.

You can search "sticky hover mobile" to discover some solutions. I came across a helpful answer on Stack Overflow here:

In my opinion, it's best not to worry about it and just leave it as it is, as that seems to be what most people are doing.

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

A Kendo editor necessitates the use of Kendo JavaScript specifically designed for Angular

Can someone provide guidance on the necessary Kendo libraries for implementing the Kendo editor in AngularJS? The tutorial site suggests that "kendo.all.min.js" is required, but I am hesitant to use it due to its resource-heavy nature. Any assistance wou ...

Rendering illuminated component with continuous asynchronous updates

My task involves displaying a list of items using lit components. Each item in the list consists of a known name and an asynchronously fetched value. Situation Overview: A generic component named simple-list is required to render any pairs of name and va ...

Struggling to show the AJAX response text in the HTML console

I am troubleshooting an issue with my ajax response text, but it keeps coming back as undefined. Here is the code I am using: $(document).ready(function() { var responseData = $.ajax({ type: "GET", url: "https://raw.githubusercontent.com/Theru ...

Use jQuery to delete the parent div when the element inside does not contain a specific class

I'm dealing with a grid that has 3 columns, each sharing the same div class. Inside each column is a span with either the class of class="simplefavorite-button" or class="simplefavorite-button.active". Here's how the markup looks: <div class= ...

What is the reason for the lack of data being displayed when using console.log() with a map object?

I currently have pet information stored in a MongoDB object like so: { "_id" : ObjectId("0000439359317900131f111"), "name" : "Travis", "description" : "Travis is a support animal", ...

What is the best way to create separate positions for items within a single div?

https://i.sstatic.net/FQRY4.jpgAfter looping through various names and accessing them from a single div using {{item}}, I encountered an issue. All the names are within one parent element. This is the code snippet: <div :style="image" cl ...

Numerical values are not considered by the JavaScript table filter

I'm having trouble with dynamically filtering the content. It works fine for the first two columns, but not for the third one. Maybe I need some additional JavaScript? Here is the link to my snippet: `https://www.w3schools.com/code/tryit.asp?filen ...

Attempting to conceal the select input and footer components of the Calendar component within a React application

I am currently working on customizing a DatePicker component in Antd and my goal is to display only the calendar body. Initially, I attempted to use styled components to specifically target the header and footer of the calendar and apply display: none; st ...

How can I recreate this image in 3D using three.js?

I have a tower image' - but i don't know how to replicate this for3dview using thethree.js` any assistance would be greatly appreciated! Here is the image : This is my attempt : $(function () { "use strict"; var container, scene, cam ...

What is the best way to check for a matching array value in my menu list and apply a corresponding class tag to it?

I need a way to dynamically add a class to tags that match specific array values. My menu list consists of 150 items, and I want to add a class to the tag whose text matches an element in the array. <ul class="nav main" id="tabs"&g ...

Navigate effortlessly with our vertical multi-level menu - simply hover over an option to reveal the next level of

This particular image demonstrates a vertical menu that is expanded. I am looking to display only the parent tag initially and upon hovering, show the next level and so forth. function display_children($parent, $level, $categoryModel) { $result = $ca ...

Learn how to showcase real-time stock information from Yahoo Finance on an Android device. See the JSON format provided below for guidance

finance_charts_json_callback( { "meta" : { "uri" :"/instrument/1.0/PTC/chartdata;type=quote;range=1d/json/", "ticker" : "ptc", "Company-Name" : "PTC Inc.", "Exchange-Name" : "NMS", "unit" : "MIN", "timezone" : "EDT", "curr ...

Tips on showcasing the elements within a div container using flexbox

Seeking advice on positioning items/cards using flexbox in my initial react app. The issue lies with the div within my card component where applying display: flex; turns every card into a block (column-like) structure, flexing only the content within each ...

Switch image on click (toggle between pause and play buttons)

Having some difficulty setting up a 3D audio player in A-Frame where the pause and play button images need to change depending on whether the audio is playing or not. Interested in seeing my code in action? Check it out here. ...

Are there any disadvantages or more efficient options for using jQuery to call .NET Page Methods?

I am currently exploring a more efficient approach to handling ajax calls in our existing .net webforms app. I have a solid understanding of jQuery and find it to be quite useful. My idea is to suggest to my team the utilization of jQuery for direct callin ...

Components from Bower are currently not available

Trying to automate the injection of bower components into my HTML using grunt-wiredep. It seems simple enough, but I'm having trouble setting the correct path to the bower directory when running localhost. Currently, I'm receiving this error: ht ...

Updating Mapped Components with Selected State

One of the components in my project is a mapped component that dynamically displays API data. Each card displayed by this component receives unique props, resulting in cards that look different from one another. An example can be seen below. View Example ...

Customizing WooCommerce: Renaming Dimensions Label

Within the WooCommerce products data table, there is a default attribute labeled dimensions that accepts values in the format LxBxH. I am interested in customizing this label from dimensions to packaging dimensions, both in the front-end and ideally in the ...

Is there a way to conceal one column in a row and display only the second column from a medium screen to a small screen using Bootstrap 5?

I am currently working on creating a row with two columns, one being the left_bar column and the other a large image. My goal is to display only the large image (column 2) when the screen size changes from medium to small breakpoints, hiding the left_bar c ...

Obtain a multiline match using regular expressions

Is there a way to use regex to match only multi-line strings without matching single-line strings as well? Here is the current regex I am using: Regex ('|"|`)[\s\S]*?(\1) Test string "not a match" "need to match&qu ...