How do I simulate a hover with a touch in touch enabled browsers?

后端 未结 14 1930
旧巷少年郎
旧巷少年郎 2020-11-22 17:04

With some HTML like this:

Some Text

Then some CSS like this:

p {
  color:black;
}

p:hover {
  color:red         


        
相关标签:
14条回答
  • 2020-11-22 17:41

    A mix of native Javascript and jQuery:

    var gFireEvent = function (oElem,sEvent) 
    {
     try {
     if( typeof sEvent == 'string' && o.isDOM( oElem ))
     {
      var b = !!(document.createEvent),
         evt = b?document.createEvent("HTMLEvents"):document.createEventObject();
      if( b )    
      {  evt.initEvent(sEvent, true, true ); 
        return !oElem.dispatchEvent(evt);
      }
      return oElem.fireEvent('on'+sEvent,evt);
     }
     } catch(e) {}
     return false;
    };
    
    
    // Next you can do is (bIsMob etc you have to determine yourself):
    
       if( <<< bIsMob || bIsTab || bisTouch >>> )
       {
         $(document).on('mousedown', function(e)
         {
           gFireEvent(e.target,'mouseover' );
         }).on('mouseup', function(e)
         {
           gFireEvent(e.target,'mouseout' );
         });
       }
    
    0 讨论(0)
  • 2020-11-22 17:41

    Use can use CSS too, add focus and active (for IE7 and under) to the hidden link. Example of a ul menu inside a div with class menu:

    .menu ul ul {display:none; position:absolute; left:100%; top:0; padding:0;}
    .menu ul ul ul {display:none; position:absolute; top:0; left:100%;}
    .menu ul ul a, .menu ul ul a:focus, .menu ul ul a:active { width:170px; padding:4px 4%; background:#e77776; color:#fff; margin-left:-15px; }
    .menu ul li:hover > ul { display:block; }
    .menu ul li ul li {margin:0;}
    

    It's late and untested, should work ;-)

    0 讨论(0)
提交回复
热议问题