Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to hover on a button and change another class but it doesnt work

I want to hover on a button and change another class but it doesn’t work, I tried multiple ways and I just cant seem to figure it out. Below is the HTML and CSS for the code.

Essentially, I’m trying to make it so when I hover over a button the circle will slowly go up to a specific amount of pixels so I decided to do a background-color test but it doesn’t work.

$(document).ready(
  () => {

    var mouseX = 0;
    var mouseY = 0;
    var xp = 0;
    var yp = 0;
    $(document).on("mousemove", function(event) {
      $(".cursor").css("left", event.pageX - 25);
      $(".cursor").css("top", event.pageY - 25);
      $(".cursor").css("display", "block");
    });
  });
* {
  margin: 0;
}

.cursor {
  height: 50px;
  width: 50px;
  background-color: lightsalmon;
  border-radius: 50%;
  position: absolute;
  display: none;
  z-index: 1;
  pointer-events: none
}

.buttonTest:hover~.cursor {
  background-color: blue;
}
<div style="padding: 1em;">
  <button class="buttonTest">Test Button</button>
</div>
<div class="cursor"></div>

<script src="https://code.jquery.com/jquery-3.5.0.js"></script>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I wee bit of javascript can facilitate this. Using JQ’s hover(), you can specify both hoverOn and hoverOff fns.

$(document).ready(
  () => {
    var mouseX = 0;
    var mouseY = 0;
    var xp = 0;
    var yp = 0;
    $('.buttonTest').hover(
      () => $('.cursor').addClass('changeColor'),
      () => $('.cursor').removeClass('changeColor'))
    $(document).on("mousemove", function(event) {
      $(".cursor").css("left", event.pageX - 25);
      $(".cursor").css("top", event.pageY - 25);
      $(".cursor").css("display", "block");
    });
  });
* {
  margin: 0;
}

.cursor {
  height: 50px;
  width: 50px;
  background-color: lightsalmon;
  border-radius: 50%;
  position: absolute;
  display: none;
  z-index: 1;
  pointer-events: none;
  transition: background-color 2s;
}

.changeColor {
  background-color: blue;
}
<div style="padding: 1em;">
  <button class="buttonTest">Test Button</button>
</div>
<div class="cursor"></div>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading