4

I've build a quiz. Each question has 2 answers. When the visitor answers a question, the next one is loaded using Ajax. Before the questions are loaded the visitor has to click a start button first, so the HTML for the questions isn't included in the initial page load.

The problem is when a question is anwsered, the hover effect from the previous question is still active when the next one is loaded.

For example: I anwser question 1 with "B" > question 2 is loaded > the hover effect is active on button B for question 2

I've included an image to make this more clear.

enter image description here

I only have this on mobile devices (iPhone, iPad,...) but not on my laptop.

I've done some research if I can alter the hover pseudo class, but apparently this isn't possible using javascript.

I think the problem is that the HTML is the same for each question, so the hover state stays active for the css class when the first question is answsered.

I can't supply a jsfiddle because the questions are entered as a content type in Drupal, and I can't include the entire Drupal in a fiddle. But here is the HTML and CSS.

<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;
}

As mentioned above, this only happens on mobile devices. I've been bashing my head at this for hours now and I'm clueless on how to resolve this.

If anyone could help me, or point me in the right direction it would be greatly appreciated.

Jrn
  • 1,185
  • 1
  • 13
  • 27
  • Could you provide us a jsfiddle perhaps to see if we can recreate the error? – Luke Nov 17 '13 at 20:12
  • 1
    This is a common issue with :hover on mobile devices i believe. Check this out, it might help you -http://stackoverflow.com/questions/2427447/does-css-hover-work-on-mobile-devices – Andy Holmes Nov 17 '13 at 20:15
  • http://labs.ft.com/2011/08/fastclick-native-like-tapping-for-touch-apps/ this might also be of use – Andy Holmes Nov 17 '13 at 20:18

2 Answers2

1

When I was working with mobile devices I added

ontouchstart=""

to the body tag like so:

<body ontouchstart="">

This made the hover pseudo selectors not act so awkwardly for me, it may be worth a shot.

Luke
  • 4,908
  • 1
  • 37
  • 59
  • +1 allthough this didn't fix my initial problem, it does help in general. Thanks – Jrn Nov 18 '13 at 11:39
1

I was able to fix this. Well,..its not really a fix because the hover state is still active, but I overwrite the color with the default color on touch devices, like so:

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

So when someone "clicks" on the quiz start button on a mobile device, my body gets the class touchdevice and I "remove" the hover with CSS, like so:

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

Technically the :hover state is still active, its just not visible anymore.

I don't really see a better way on fixing this at the moment. If someone does, please let me know.

Jrn
  • 1,185
  • 1
  • 13
  • 27