CSS hover effect on ajax content and mobile devices

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.

在这里输入图像描述

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.


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.


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.

链接地址: http://www.djcxy.com/p/49662.html

上一篇: 在WkWebView / mobile Safari的页面中为iframe启用会话cookie

下一篇: Ajax内容和移动设备上的CSS悬停效果