With JavaScript/jQuery I can create events for when the mouse enters or leaves an element using mouseenter
and mouseleave
. For example:
var img = document.querySelector("img");
img.addEventListener("mouseenter", function() {
console.log("Estoy sobre la imagen");
});
img.addEventListener("mouseleave", function() {
console.log("Ya no estoy sobre la imagen");
});
<img src="http://placehold.it/200" />
This will cause actions to be executed (display messages on the console) when the mouse enters or leaves the image. Now my question is: is there an event that instead of being fired when the mouse enters the image, is fired when the mouse is in the proximity of the image?
For example, if I have an image (although it could potentially be anything like a div
or a thead
) next to some text, and the mouse comes within 30px of the image's border, an action is executed in JavaScript. This graphic shows more or less what I want - something should happen if the mouse enters the area around the image (highlighted in red for example only, it's an "imaginary" area):
If it doesn't exist, how could that event be simulated with JavaScript or jQuery?
I just cobbled together the code so it may be buggy, but I think something like this might do the trick.
Calculating the distance between the mouse and the object to decide if the mouse is on the image, near or far from it.
The variable
distanciaMinima
sets the minimum distance at which the mouse is considered to be close to the image.you can add a div element and with the padding property you specify the range of approach to the image, and add the mouseenter event, it would be something like this:
INTRODUCTION:
To do it with pure javascript we will do the following: We will create a function so that when a mouse event is triggered (in this case onmousemove) it checks the distance between the mouse and the object (both already defined) and in the case of being fulfilled it would the action we want.
REQUIRED PARAMETERS:
Therefore we would need three parameters:
METHODS AND FUNCTIONS TO USE:
We already have the parameters ready, the next thing would be to obtain the four sides of the object, we will do this with
offset
, which allows us to obtain the position in pixels of the margin or limit part of the element.We will have two possibilities
offsetLeft
andoffsetTop
.It would be necessary to define the bottom and right part of the object, so we will use:
width
to get the width (this being used to calculate the right margin)height
the height (to calculate the lowest point)GRAPH FOR BETTER UNDERSTANDING:
Let's first think about this plot of the X/Y axes:
With which we will be guided in the calculations for the limit of the object.
DEFINING THE LIMITS:
LEFT:
Now we start then to create the sides of the object:
We get the value of the left margin and then we subtract the distance to create the limit that we want with the object with respect to the left side, if we subtract it the limit position will be shifted to the left.
HIGHER:
We get the value of the top margin and then we subtract the distance to create the limit that we want with the object with respect to its height.
LAW:
Now let's go with the calculated sides:
For the right we will use the position of the left margin and then we will add the width of the object, this would result in the right margin, and then we add the distance that we want from the limit.
The multiplication is used, because the limit with the distance without multiplying would be right in the margin of the object.
LOWER:
For the bottom we will use the position of the top margin and then we will add the height of the object, this would result in the right margin, and then we add the distance that we want from the limit.
Multiplication is used, because the limit with the distance without multiplication would be right at the edge of the object.
FINAL LIMITS:
Leaving our limits like this:
MOUSE POSITION:
Next we need to get the position (coordinates) of the mouse for both the X and Y axis.
For this we will use
pageX
andpageY
, which will react with the event (onmousemove)Let's save like this:
Where event will be:
CHECKING DISTANCE:
We will see how to check the position of the mouse in relation to the object and the Cartesian plane, with which the following must be true:
Now we will use a return to know if it is fulfilled or not, as follows:
END FUNCTION:
TRIGGER MOUSE EVENT:
We will declare within our JavaScript file the function to calculate when the event occurs
onmousemove
, looking like this:END CODE:
With this our function would be ready
LET'S ADD STYLE TO SEE IT BETTER:
CREDITS:
You just have to add a div with a specific size and add padding to the div:
Example if your image is 100x100 px, your div will be 200x200 px and so you add a 100px padding to the div and you have that range for what you want to do, you add the event to the div and it would be everything.
And with this it should work without, I didn't test the code but it should work adapted to your needs.