How do I remove :hover?

前端 未结 9 785
南方客
南方客 2020-12-15 03:41

I have a small problem with a script.
I want to have a default action on :hover for clients with Javascript disabled, but for those with Javascript enabled

9条回答
  •  心在旅途
    2020-12-15 04:32

    I HAVE FOUND YOUR SOLUTION

    basically you start out by redefining what you did with the css hover. (naturally you would do this by dynamically pulling the information from the style) then do whatever you want to in jquery with mouseover/mouseout events

    this allows you to keep the :hover event in your css because jquery is binding your original styles to the element. In essence disabling the :hover event.

    if your css is:

    a.class {
      background-color: #000000;
      background-position: 0 0;
      }
    a.class:hover {
      background-color: #ffffff;
      background-position: 100% -50px;
      }
    

    your jquery would be somthing like:

    jQuery("a.class").each(function () {
    
      var oldBackgroundColor = jQuery(this).css("backgroundColor");
      var oldBackgroundPosition = jQuery(this).css("backgroundPosition");
    
      jQuery(".class").css({
            'backgroundColor':oldBackgroundColor,
            'backgroundPosition':oldBackgroundPosition
      });
    
    })
    .bind("mouseover", function() {
    
      doSomething();
    
    })
    .bind("mouseout", function() {
    
      doSomething();
    
    })
    

提交回复
热议问题