How to get the mouseovered half of an element
You can also be interested in:
Today I'll write a very simple and short post about a very simple function that you can use to retrieve in which half (left/right) of an element the mouse is moving towards. The function is written in vanilla javascript, no frameworks are involved.
Given the element which captures the event and the event itself the function return the string 'left' or 'right' depending on the cursor position over the element.
/*
* Gets the mouseovered half of an element
* @param {Element} element: the element which captures the event
* @param {Event} evt: the event object
* @return the mouseovered half ('left' | 'right')
*/
var getMousemoveHalf = function(element, evt) {
var element_coords = element.getBoundingClientRect(),
pointer_coords = {x: evt.clientX, y: evt.clientY};
// only gecko sets the widh property
return pointer_coords.x < (element_coords.left + (element_coords.right - element_coords.left) / 2) ? 'left' : 'right';
}
All the magic is done by the getBoundingClientRect function, which has a good browser compatibility.
Your Smartwatch Loves Tasker!

Your Smartwatch Loves Tasker!
Featured

Django admin and bootstrap 5
Bootstrap 5 has come, currently in beta release, and seems already very stable.
So the question is: are you looking for ...

About code optimization, learn from exercises
Let's see an example of exercise you can face during a job interview, and let's use it to understand some ...

Notes on the Pearson correlation coefficient
The Pearson correlation coefficient is a measure of the linear correlation between two variables X and Y. It has a ...
Archive
- 2021
- 2020
- 2019
- 2018
- 2017
- Nov
- Oct
- Aug
- Jun
- Mar
- Feb
- 2016
- Oct
- Jun
- May
- Apr
- Mar
- Feb
- Jan
- 2015
- Nov
- Oct
- Aug
- Apr
- Mar
- Feb
- Jan
- 2014
- Sep
- Jul
- May
- Apr
- Mar
- Feb
- Jan
- 2013
- Nov
- Oct
- Sep
- Aug
- Jul
- Jun
- May
- Apr
- Mar
- Feb
- Jan
- 2012
- Dec
- Nov
- Oct
- Aug
- Jul
- Jun
- May
- Apr
- Jan
- 2011
- Dec
- Nov
- Oct
- Sep
- Aug
- Jul
- Jun
- May