Disable :hover on click

后端 未结 5 1333
醉梦人生
醉梦人生 2021-01-31 20:18

I have a div that has hover:

.div {
  // css
}
.div:hover {
  // css
}

But I want to disable the hover when you click on the

相关标签:
5条回答
  • 2021-01-31 20:22
    .disabled {
    
      opacity: 0.5;
      pointer-events: none;
    
    }
    
    .disabled:hover *
    
      pointer-events: none;
      cursor: not-allowed;
    
    }
    
    0 讨论(0)
  • 2021-01-31 20:24

    Try this:

    JQUERY

    $(function() {                       //run when the DOM is ready
      $(".div").click(function() {  //use a class, since your ID gets mangled
        $(this).addClass("active");      //add the class to the clicked element
      });
    });
    

    CSS

    .div.active:hover {
    cursor:default;
    //same CSS as the DIV
    
    }
    
    0 讨论(0)
  • 2021-01-31 20:26

    All you need is this:

    $('[your selector]').css({'pointer-events': 'none'})
    
    0 讨论(0)
  • 2021-01-31 20:30

    Try like this:

    $('#myid').on({
        mouseover: function(){
            $(this).css({background: '#F94'});
        },
         mouseleave: function(){
            $(this).css({background: '#DDD'});
        },
        click: function(){
            $(this).off('mouseleave');
        }
    });
    

    JSFIDDLE DEMO

    0 讨论(0)
  • 2021-01-31 20:42

    Option 1. The Javascript solution

    Simply add a class prohibiting the application of hover styling on click:

    $('div').on('click', function() { // when you click the div
      $(this).addClass('no-hover'); // add the class 'no-hover'
    });
    div {
      color: blue;
    }
    div:not(.no-hover):hover { /* only apply hover styling when the div does not have the class 'no-hover' */
      color: red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div>hover!</div>

    Option 2. The CSS solution

    Alternatively, in pure CSS (although without rule persistence)

    div {
      color: blue;
    }
    div:not(:focus):hover {
      color: red;
    }
    div:focus {
      outline: none;
    }
    <div tabindex="0">hover!</div>

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