"Enhancing user experience with CSS hover effects on dynamically loaded content and mobile devices

I have developed a quiz with 2 answer options for each question. To prevent the questions from loading initially, visitors must click a start button, triggering the questions to load via Ajax.

The issue I am facing is that when a question is answered, the hover effect from the previous question remains active on the next question that is loaded.

For instance: If I answer question 1 with "B", then question 2 is loaded and the hover effect is still active on button B for question 2.

To illustrate this problem more clearly, I have attached an image.

This issue only occurs on mobile devices (iPhone, iPad, etc.) and not on my laptop.

I have tried to modify the hover pseudo class using JavaScript, but it seems that this is not achievable.

I suspect that the repetitive HTML structure for each question is causing the hover state to persist for the CSS class when transitioning from one question to another.

Unfortunately, I cannot provide a JSFiddle as the questions are entered as a content type in Drupal, making it impossible to include the entire Drupal setup in a fiddle. However, I have included the HTML and CSS for reference.

<div class="quiz_st_content form-wrapper" id="ajax_form_multistep_form_content">
  <div class="quiz_st_content_answer form-wrapper" id="edit-a--2">
    <div class="quiz_st_content_answer_info_wrapper">Option A</div>
    <div class="quiz_st_content_answer_button_wrapper">
      <input class="quiz_st_content_answer_button form-submit ajax-processed" type="image" id="edit-answer-a-2" name="answer_a_2">
    </div>
  </div>
  <div class="quiz_st_content_answer form-wrapper" id="edit-b--2">
    <div class="quiz_st_content_answer_info_wrapper">Option B</div>
    <div class="quiz_st_content_answer_button_wrapper">
      <input class="quiz_st_content_answer_button form-submit ajax-processed" type="image" id="edit-answer-b-2" name="answer_b_2">
    </div>
  </div>
</div>

CSS

input.form-submit.quiz_st_content_answer_button {
  margin: 0;
  border-radius: 50px;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  height: 30px;
  width: 30px;
  padding: 20px;
  background: #ccc;
}

Hover

input.form-submit.quiz_st_content_answer_button:hover {
  background: #ba043f;
}

This issue has been causing me frustration for hours, and I am at a loss on how to fix it.

If anyone can offer assistance or guidance on how to address this issue, I would greatly appreciate it.

Answer №1

While experimenting with mobile devices, I decided to enhance the user experience by including the following code snippet:

ontouchstart=""

I inserted this code into the body tag like this:

<body ontouchstart="">

By doing this, I found that the hover pseudo selectors behaved more smoothly for me. It might be worth trying out for yourself.

Answer №2

After some tinkering, I managed to come up with a workaround for this issue. Although it's not a complete solution as the hover state remains active, I found a way to override the color on touch devices. Here's how I did it:

$('.quiz_st_form,').bind('touchstart', function(){
  $('body').addClass('touchdevice');
});

By adding the class touchdevice to my body when the quiz start button is "clicked" on a mobile device, I then used CSS to eliminate the hover effect, like this:

body.touchdevice input.form-submit.quiz_st_content_answer_button:hover {
  background: #ccc;
}

Although the :hover state technically persists, it is no longer visible to the user.

At the moment, this is the best solution I could come up with. If anyone has a more effective approach, I would appreciate any suggestions.

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

Establish a trigger in mongoDB that monitors changes in document values and triggers an email notification

Hello there! As a newcomer to MongoDB and the world of web development, I have a question that I hope you can help me with. I am looking to send out emails based on the time difference between the current time and the event time stored in the event model a ...

When ALIGN-ITEMS: CENTER is removed, the <a> element expands to fill the entire line

Currently, I am delving into the world of CSS and grappling with a particular behavior that has caught my attention. section{ display: flex; flex-direction: column; align-items: center; } .banner-area a.banner-btn{ padding: 15px 35px; backgrou ...

Troubleshooting MySQL through PHP for errors

I've developed a comment posting system where users can write and submit comments using PHP, MySQL, jQuery, AJAX, and JSON. However, I encountered an issue with JSON insertion while debugging the system with Firebug. The error message displayed was: ...

Could it be a potential issue with array sorting in Chrome and IE?

My array sorting script works perfectly on Firefox, however, Chrome and IE are giving me an unordered array. var arr = ["2017-02-17", "2017-02-17", "2017-02-16", "2017-02-15", "2017-02-16", "2017-02-15", "2017-02-14", "2017-02-16", "2017-02-17", "2017-02- ...

What is the function of the "AJAX_MODE" parameter with a value of "Y"?

I have come across conflicting information regarding the functionality of "AJAX_MODE"=>"Y" in Bitrix components. The resources at and various articles on AJAX in Bitrix do not provide a clear explanation. Can someone clarify what exactly this configu ...

Why is the background image not loading properly on first hover with CSS?

Whenever we load a page with a large image set as the background for a div, there seems to be a delay in displaying the image when we hover over the div. Is there a solution to this issue? I would prefer not to use a sprite image because I need to make alt ...

Is there a way to set up a listener for a particular property of a recoil state object?

Is it possible to create a listener for a specific property within a state object? Let's look at an example: type ExampleAtomProperties = { id: number; description: string; }; const ExampleAtom = atom< ExampleAtomProperties>({ key: &quo ...

Make sure to close any existing Featherlight windows before trying to open another one

I'm setting up multiple featherlight instances when the page loads jQuery('.feedback').featherlight(jQuery( "#feedback-box" ), { closeIcon: 'close'}); jQuery('#imprint').featherlight(jQuery( "#imprint-box" ), { closeIcon ...

Interactive section for user input

I am looking to add a commenting feature to my website that allows for dynamic editing. Essentially, I want users to be able to click on an "Edit" span next to a comment and have it transform into an editable textarea. Once the user makes their changes and ...

CSS Background Color Not Displaying Correctly

My HTML5 page has an issue where the image is taking over the background color of the entire page. I want to adjust it so that the image appears in the background, while the background color shows up behind the content on top. Check out my code below and l ...

NuxtJS using Babel 7: the spread operator persists in compiled files

Struggling to get my NuxtJS app functioning properly on IE11. Despite multiple attempts to configure Babel for compatibility, spread operators are still present in the built pages files, suggesting that Nuxt code is not being transformed correctly. Below ...

CSS linking causing pages to crumble

Out of nowhere, while working on my project today, my page suddenly collapsed. It was a frustrating sight to see. Immediately, I went back to Dreamweaver and used cmd+z to undo a few steps, but there was no visible issue. Despite this, my page continued t ...

Capturing attention within react-bootstrap drop-downs and pop-ups

Currently, I'm focused on enhancing accessibility features and I have a specific goal in mind: to confine the focus within a popover/dropdown whenever it is opened. Working with react-bootstrap, my inquiry revolves around finding out if there's ...

Explore the steps for setting up automatic reconnection in socket.io

When establishing a socket.io connection, I use the following code: var socket = new io.connect('http://localhost:8181', { 'reconnect': true, 'reconnection delay': 500, 'max reconnection attempts': 50 }) ...

Looking for a custom textured circle in three.js?

Is there a way to create a circle with a texture on one side in three.js without using sprites? I was considering using THREE.CylinderGeometry, but I'm unsure of where to add the "materials" in the new THREE.CylinderGeometry(...) section. Any suggest ...

Comparison between Standard Form Submission and Ajax Form Submission: Leveraging Browser's Auto Save and User Input Suggestions

Many of us who use forms with ajax have likely encountered the issue where the user input is not saved or suggested by the browser in future sessions. This can be problematic, especially on 'sign in' pages where the browser typically offers to sa ...

Reduce the size of several JavaScript files using webpack

Hello, I am a beginner with webpack and I am looking to minify all the files in a folder into a single js file using webpack. I want to avoid adding each file manually in the entry section. I believe there must be a more efficient way to achieve this, bu ...

What is the best way to divide my HTML page in half?

My goal is to display two maps side by side, one on the right and the other on the left. However, when I try to implement this, one map appears on top of the other. I believe that CSS positioning can help me achieve the desired layout, but as a beginner in ...

Menu with a full-width fluid input field using semantic-ui

I am trying to create a fixed top menu with an image on the left, an input to its right, and another input on the far right. My goal is to have the center input stretch to fill all remaining space between the image and the other input. Although I would no ...

"Troubleshooting problems with jQuery accordion collapse and styling using CSS

I'm currently dealing with an issue regarding my accordion design. I have customized the stylesheet using themeroller, but when I collapse one of the sections, the header changes to black instead of reverting back to its original red color. I've ...