Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if mouse is over a column border

Does anybody know how can I detect if mouse is over a column border or a cell border, either by jQuery or JavaScript?

I want to implement a column resizing on a specific table.

Any help is appreciated.

like image 767
Javad Avatar asked Mar 14 '14 16:03

Javad


People also ask

How do you know if a mouse is over an element?

Use document. elementFromPoint(x, y) method to get the element content on that position when mouse pointer moves over.

Is mouse over jQuery?

jQuery mouseover() Method The mouseover() method triggers the mouseover event, or attaches a function to run when a mouseover event occurs. Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element.


2 Answers

You should check if the offsetX and offsetY are less than the border-width and if so you're in the border, also check if offsetX is greater than innerWidth or offsetY is greater than innerHeight

$('td').hover(function(e){
    var border_width = parseInt($(this).css('border-width'));
    if(e.offsetX < border_width || e.offsetX > $(this).innerWidth() || e.offsetY < border_width || e.offsetY > $(this).innerHeight()){
        console.log('This is the border');  
    }
});

Here's a jsFiddle

like image 109
Ohgodwhy Avatar answered Oct 01 '22 06:10

Ohgodwhy


May be you can do like as follows:

  1. Listen for mouse hover event on the element
  2. Check the mouse position.
  3. If the offset() + outerWidth() of the element is same as the mouse position, it means, you are on the border.
like image 22
Jinto Avatar answered Sep 29 '22 06:09

Jinto