Home > other >  JavaScript hover event on a vendor specific pseudo-elements
JavaScript hover event on a vendor specific pseudo-elements

Time:12-08

I have the following html input tag.

$("input[type='range']::-webkit-slider-thumb").on('hover', function () {
    //...
});
input[type="range"] {
    height: 0.5rem;
    box-shadow: 0 0 0.25rem gray;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    border: 0;
    width: 1.25rem;
    height: 1.25rem;
    border-radius: 100%;
    background: #7CB5EC;
    box-shadow: 0 0 0.375rem gray;
    cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
    -webkit-appearance: none;
    appearance: none;
    border: 0;
    width: 1.25rem;
    height: 1.25rem;
    border-radius: 100%;
    background: #7CB5EC;
    box-shadow: 0 0 0.375rem gray;
    cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<input id="my-range" type="range" min="0" max="100">

I would like to get hover events via JavaScript whenever the user hovers only on the thumb.

I tried to do it like above but it doesn't trigger the event. Any help is appreciated.

Thanks in advance.

CodePudding user response:

I strongly believe that you can't do this with the default html input range, you will have to use a custom one, like this:

 $("#slider").slider({
  range: "min",
  min: 0,
  max: 100,
  change: function( event, ui ) {
      console.log(ui.value);
  }
});

$(".ui-slider-handle").on("mouseover", function() {
  console.log("Hover on thumb!");
})
#slider{
  width: 200px;
  margin-left: 20px;
  border-radius: 100px;
  height: 10px;
  background: #efefef;
}

.ui-slider-handle {
  background: #0976f7 !important;
  border-radius: 100px !important;
}

.ui-widget-header{
  background: #0976f7 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<div id="slider"></div>

  • Related