Persistent hover effect for collapsible accordion panels when closing them

My simple accordion features a functionality where a class of .open is added to the title + content group when you open a panel for styling purposes. Everything works smoothly, but I've noticed on my phone that after clicking to close a panel, the hover styling (which matches the active/open styling) remains. This inconsistency is especially evident due to the plus/minus icon usage. Clicking elsewhere on the page removes this lingering styling. Since there is no :focus styling, could it be that the :hover action is being interpreted as a touch event? Any suggestions on how I can address this issue?

Below is the script I am using:

$(document).ready(function($) {
    // Add class of `.open` to first `.accordion__title` since it's set to `display: block` in CSS.
    $('.accordion__item:first-child .accordion__title').addClass('open');
    // Accordion functions.
    $('.accordion').find('.accordion__title').click(function(){
        // Add class to title.
        $(this).toggleClass('open');
        // Expand or collapse this panel.
        $(this).next().slideToggle('fast');
    });

});

The HTML markup for the accordion:

<div class="accordion">

    <div class="accordion__item">
        <h2 class="accordion__title">Question One <span class="accordion__title-icon"></span></h2>
        <div class="accordion__copy">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
        </div>
    </div>

    <div class="accordion__item">
        <h2 class="accordion__title">Question Two <span class="accordion__title-icon"></span></h2>
        <div class="accordion__copy">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        </div>
    </div>

    <div class="accordion__item">
        <h2 class="accordion__title">Question Three <span class="accordion__title-icon"></span></h2>
        <div class="accordion__copy">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
        </div>
    </div>

</div>

You can also view a demonstration on CodePen: https://codepen.io/moy/pen/aENWXz

If you inspect the same effect on Chrome using any mobile device through Browser Tools, you'll notice the issue.

Answer №1

Did you know that it's possible to modify the appearance of hover effects on touchscreen devices by implementing a media query?

@media (hover: none) {
    .accordion__title {
        &:hover{
            color: #000;
        }
        &.open {
            color: #c8102e;
        }
    }
}

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

Is it possible to utilize a route path in a JavaScript AJAX request?

So I have a situation with an ajax call that is currently functioning in a .js file, utilizing: ... update: function(){ $.ajax({ url: '/groups/order_links', ... However, my preference would be to use the route path instead. To achieve ...

When an identical Ajax request is made multiple times, Safari will block JQuery

I'm having trouble getting my progress bar to update in Safari (8.0). While uploading a file, the progress bar updates automatically in Firefox and Chrome, but not in Safari. I suspect this may be due to Safari not handling synchronous events, causing ...

Discovering a specific element using jQuery

I am trying to work with a div structure like this: <div class="country"> <div class="cty_popover"> <p>TITLE</p> <ul> <li>NAME 1</li> <li>NAME 2</li> ...

What is the best way to replicate the functionality of AngularJS decorators in pure vanilla JavaScript (ES6)?

Using AngularJs Decorators offers a convenient method to enhance functions in services without altering the original service. How can a similar approach be implemented with javascript classes? In my current project, I have two libraries - one containing g ...

Obtain information stored locally when an internet connection is established

I'm currently facing an issue with my Local Storage data retrieval process in Vuejs while being online. My ToDo app setup consists of Vuejs, Laravel, and MySQL. When the internet connection is available, I store data in localStorage. The ToDo app has ...

Speedily deliver a message to the designated destination

I have a specific route set up at /test: app.route('/test', (req,res)=>{ res.sendFile(__dirname + "\\myhtml.html") }) Now, I want to trigger an event in Node.js on the /test route, and have myhtml.html file listen for ...

Automate your Excel tasks with Office Scripts: Calculate the total of values in a column depending on the criteria in another column

As a newcomer to TypeScript, I have set a goal for today - to calculate the total sum of cell values in one column of an Excel file based on values from another column. In my Excel spreadsheet, the calendar weeks are listed in column U and their correspon ...

Using AngularJS in conjunction with Ruby on Rails is causing compatibility issues

Trying to implement Angular with Ruby on Rails is presenting some challenges. While simple expressions like 1+1 work fine, binding a number or string to a scope seems to be causing issues. I am looking for suggestions on how to resolve this problem. app. ...

Utilizing MongoDB Data in an .ejs Template Using Node.js Express

After going through numerous tutorials, I find myself stuck at a point where I am struggling to render all the data written by my express-app into MongoDB in embedded JavaScript. My goal is to display this data in a simple table that always shows the updat ...

Issue with AngularJS UI Bootstrap Datepicker not opening again after clicking away

I've integrated Angular UI bootstrap with my AngularJS project, utilizing version 0.11.0. While implementing various directives from the library, I encountered an issue when trying to incorporate the date picker. There are instances where multiple da ...

What is the best way to show my button only when within a specific component?

Is there a way to display the Logout button on the same line as the title only when the user has reached the Home component? In simpler terms, I don't want the logout button to be visible all the time, especially when the user is at the login screen. ...

invoke a function upon successful completion of an ajax call in a datatable

Can we trigger a JavaScript function after a successful AJAX call in a datatable? Here is the code I am attempting to use: var dataTable = $('#app-config').dataTable( { "bAutoWidth": false, ...

Creating a custom Chrome extension with CSS embedded within an iframe

Recently, I developed a Chrome app that includes an iframe element within it. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="v ...

CSS can be used to eliminate specific background images from a webpage

In the CSS code, I have specified two background images within a 'body' tag. #body { background: url("image1.jpeg"), url("image2.png"); background-repeat: no-repeat, no-repeat; } But now, I need to remove only "image1" from a specific p ...

What is the process for generating an HTTP response that results in a pipe error being thrown

In my NestJS application, I have created a custom pipe that validates if a given value is a valid URL. If the URL is invalid, an error is thrown. This pipe is utilized in a controller to save an item into the database. Recently, I discovered that the pipe ...

Attempt to efficiently register components automatically on a global scale in Vue by utilizing the powerful combination of Laravel-Mix and Webpack

In my previous projects with Vue, which were based in a Laravel-Mix/Webpack runtime environment, I used to individually register components and views as needed. This was done by importing them and extending Vue: import BaseButton from './components/Ba ...

What is the best way to compare two strings without considering their capitalization?

Here is the issue I am facing: mytext = jQuery('#usp-title').val(); Next, I check if the text matches another element's content: if(jQuery("#datafetch h2 a").text() == mytext) { However, titles can vary in capitalization such as Space Mi ...

Is the node certificate store limited to reading only from a predefined list of certificates?

Is there a way to add a new certificate to the list of certificates node trusts, even after some struggle? It appears that Node only trusts certificates hardcoded in its list located here: https://github.com/nodejs/node/blob/master/src/node_root_certs.h ...

Avoiding Duplicate Paths in URLs with Perl CatalystOne common issue that can occur in web development is the presence of double

I recently developed a web application using Catalyst framework. The login page can be accessed by typing the URL: http://mydomainname/login When accessing this URL, the login page is displayed beautifully with all its styling from the CSS file. However ...

Updating the .css file through the graphical user interface, within the managed bean

I have created a jsf page with colorpickers to allow an administrator to modify the main theme of the site, which consists of 4 colors. I have prefixed my main colors in the css file with numbers like this: .element{ background: /*1*/#333333; } When ...