Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I position a div next to a mouse click using JQuery?

Tags:

How do I position a div next to a mouse click using JQuery?

Thanks

like image 247
Nasir Avatar asked Aug 19 '10 11:08

Nasir


People also ask

How to get position of div in jQuery?

jQuery position() MethodThe position() method returns the position (relative to its parent element) of the first matched element. This method returns an object with 2 properties; the top and left positions in pixels.

What is the difference between position and offset in jQuery?

Difference between offset() and position() Method:The jQuery UI offset() is relative to the document. The jQuery UI position() is relative to the parent element. When you want to position a new element on top of another one which is already existing, its better to use the jQuery offset() method.

Can you give a div an onclick?

The answer is definitely yes, but you will need to write javascript code for it. We can use a click handler on the div element to make it clickable.

How can you tell if a mouse is left or right jQuery?

Using the mousedown() method: The mousedown() method in jQuery can be used to bind an event handler to the default 'mousedown' JavaScript event. This can be used to trigger an event. The event object's 'which' property can then used to check the respective mouse button.


1 Answers

You can try:

$( "td").click( function(event) {   $("#divId").css( {position:"absolute", top:event.pageY, left: event.pageX}); }); 

After additional question was asked in the comment:

$( "td").click( function(event) {   var div = $("#divId");   div.css( {       position:"absolute",        top:event.pageY,        left: event.pageX});    var delayTimer = setTimeout( function( ) {         $that.fadeIn( "slow");      }, 100);    div.mouseover( function( event) {      if (delayTimer)          clearTimeout( delayTimer);   }).mouseout( function(){      if (delayTimer)          clearTimeout( delayTimer);      var $that = $(this);      delayTimer = setTimeout( function( ) {         $that.fadeOut( "slow");      }, 500)            }); }); 
like image 81
Artem Barger Avatar answered Sep 30 '22 15:09

Artem Barger