JavaScript jQuery mouseenter is just flickering the img

I am working on a project where I want to display an image and when the mouse moves over the image, the image disappears and info on the page appears. When the mouse moves out of the element, the image reappears. I am using mouseenter and mouseleave but the image keeps flickering if the mouse if moved even slightly.

$(".second-div").mouseenter(function() {
  $(this).removeClass("second-div");
});
$(".second-div").mouseleave(function() {
  $(this).addClass("second-div");
});
.first-div {
  height: 200px;
  width: 300px;
  border: 1px solid black;
}

.second-div {
  height: 200px;
  width: 300px;
  border: 1px solid red;
  position: relative;
  bottom: 201px;
  background-image: url(https://image.freepik.com/free-vector/nature-background-of-a-grassy-landscape_1048-1305.jpg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div class="first-div">

</div>

<div class="second-div">

</div>
Answer:1

There is no need for JS or jquery, you can do this with CSS. Just toggle the opacity of the top element on :hover

.first-div {
  height: 200px;
  width: 300px;
  border: 1px solid black;
}

.second-div {
  height: 200px;
  width: 300px;
  border: 1px solid red;
  position: relative;
  bottom: 201px;
  background-image: url(https://image.freepik.com/free-vector/nature-background-of-a-grassy-landscape_1048-1305.jpg);
  transition: opacity .25s;
}

.second-div:hover {
  opacity: 0;
}
<div class="first-div">
  content
</div>
<div class="second-div">
</div>
Answer:2

I agree that the CSS solution is much better and cleaner. But below is what I believe you were trying to accomplish with jQuery.

In your original question, you are adding/removing the second-div class which was causing your mouse events to fire erratically.

$(".second-div").mouseenter(function() {
  $(".second-div").hide();
});
$(".first-div").mouseleave(function() {
  $(".second-div").show();
});
.first-div {
  height: 200px;
  width: 300px;
  border: 1px solid black;
}

.second-div {
  height: 200px;
  width: 300px;
  border: 1px solid red;
  position: relative;
  bottom: 201px;
  background-image: url(https://image.freepik.com/free-vector/nature-background-of-a-grassy-landscape_1048-1305.jpg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div class="first-div">

</div>

<div class="second-div">

</div>
Answer:3

I am pulling some text from a database and I have , and I would like to replace them with <br> don't really know why it's not working any ideas? JS Code $( document ).ready(function() { //....

I am pulling some text from a database and I have , and I would like to replace them with <br> don't really know why it's not working any ideas? JS Code $( document ).ready(function() { //....

I need to make the array 'places' display its contents in the list. My problem is my function. I know I'm not doing it right. Can anyone help? Do I need to use getElementsByTagName? <body> ...

I need to make the array 'places' display its contents in the list. My problem is my function. I know I'm not doing it right. Can anyone help? Do I need to use getElementsByTagName? <body> ...

  1. make this array
  2. make array python
  3. make array golang
  4. make array java
  5. make array javascript
  6. make array matlab
  7. make array php
  8. make array unique javascript
  9. make array c++
  10. make array in jquery
  11. make array numpy
  12. make array empty javascript
  13. make array js
  14. make array strictly increasing
  15. make array from string
  16. make array of zeros python
  17. make array from object javascript
  18. make array from string php
  19. make array from string javascript
  20. make array bash

I am testing emit and listen methods with VueJS to learn how to use it. I get some strange results, I don't understand. I would expect my function initMap() is called and the console is logging, which ...

I am testing emit and listen methods with VueJS to learn how to use it. I get some strange results, I don't understand. I would expect my function initMap() is called and the console is logging, which ...

  1. vuejs event handler
  2. vuejs event handler parameters
  3. vuejs event handler component
  4. vuejs event listener
  5. vuejs event listener on component

I have been unable to find any examples of RSA-OAEP encryption in IE11. Here's a snippet of my implementation, in which I am given a very vague error. function convertStringToArrayBufferView(str)...

I have been unable to find any examples of RSA-OAEP encryption in IE11. Here's a snippet of my implementation, in which I am given a very vague error. function convertStringToArrayBufferView(str)...