How do I position a div next to a mouse click usin

2020-02-08 11:08发布

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

Thanks

2条回答
家丑人穷心不美
2楼-- · 2020-02-08 11:50

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)         
  });
});
查看更多
淡お忘
3楼-- · 2020-02-08 12:01

Something like:

$('#cell').bind('click',
    function(e){
        $('#div').css('left',e.pageX + 'px' );
        $('#div').css('top',e.pageY + 'px' ); });

The div's position should be set to absolute.

查看更多
登录 后发表回答