Search code examples
jquerycursor-position

How to get cursor coordinates inside a specific div?


$('#imgSW').click(function (e) {
var w=window.innerWidth/2;
if (e.pageX > w)(changeUp())
else(changeDown());
});

This worked fine when #imgSw was horizontally centered.
But now, it is placed inside a right div, with liquid width.
How can I get the cursor coordinates just referring the #imgSW, not the window.


Solution

  • Change your var w for that :

    var $this = $(this)
    var w=$this.offset().left+($this.width()/2);
    

    And it should work.