-3

I am having trouble making elements hover on a mobile device.

In my CSS, I type something like:

.button:hover {
   background-color: #fff;
}

This seems to hover fine on my desktop, however on my iPhone and other touch devices, I can't seem to make the button hover.

TylerH
  • 20,799
  • 66
  • 75
  • 101

3 Answers3

2

There is no hover event on mobile, you can define hover behavior and it will work on desktop. But on mobile, you will see this hover only by click/touch.

P.S.
  • 15,970
  • 14
  • 62
  • 86
-1

Hovers mean one thing on the other. in this case its the mouse cursor over the HTML button or whatever element you use.

In the case of a phone, there is no mouse cursor. its always a click. Hovers in most cases would show only on click or touch. If you really need this you can try the Javascrit onclick() or other functions, so when they click or touch, you can add a bit of something.

mw509
  • 1,957
  • 1
  • 19
  • 25
  • I checked both links on my 5.5 inch android 7 chrome browser and they all acted the same. The background changes after I click. While I hold the click the background does not change. Best reason and explanation I can give is devices and browsers all react differently to these things. While some may try as much as possible to replicate experiences, others just know the two devices are not the same and keep them apart. so my advice is focus on universal features than a few. Hovers just do not work like that. You will have to use javascript to play the trick like I said. – mw509 Sep 06 '17 at 10:48
  • you can a run some tests from this site it simulates several instances for you [link] (http://www.mobilephoneemulator.com/) – mw509 Sep 06 '17 at 10:52
-1

For a very quick solution of this problem with jQuery/CSS, I did the following.

  1. wrote styles for my element like this:

    .one-block:hover, .one-block.hover { ... }
    
  2. positioned it:

    .one-block { position: relative; }
    
  3. added the last element in this block -> .mobile-mask:

    <div class="one-block">
        ....
        <div class="mobile-mask"></div>
    </div>
    
  4. positioned .mobile-mask:

    .mobile-mask {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
    }
    
  5. hide it for non-mobile (this part is not precise at all: just with css. if you detect mobile devices by some other means and, for example, give a class to body, than it will be more precise. in this case I just need a quick solution):

    @media (min-width: 681px) {
      .mobile-mask {
        display: none;
      }
    }
    
  6. create javascript that will add/remove .hover class:

    $('.mobile-mask').on('click', function(){
      $('.list .mobile-mask').show();
      $('.list .one-block').removeClass('hover');
      $(this).hide();
      $(this).parent().addClass('hover');
    });
    $(window).on('resize', function(event){
      var windowWidth = $(this).width();
      if(windowWidth > 680){
        $('.list .one-block').removeClass('hover');
      }
    });
    
kakaja
  • 714
  • 2
  • 9
  • 22