How to position 400 x 400px popup div relative to click, keeping in screen view

后端 未结 2 661
无人及你
无人及你 2021-01-06 06:07

Preferably with jQuery since it\'s so much easier to understand.

The following works great for horizontal. The issue is vertical can be cut off on top requiring the

相关标签:
2条回答
  • 2021-01-06 06:16

    Something like this should work:

    $(document).click(function (e) {
    
      var x = e.clientX,
      y = e.clientY,
      width = $(window).width(),
      height = $(window).height();
    
      if( x > width - 400 ) x -= 400;
      if( y > height - 400 ) y -= 400;
    
      $('#popup').css({'top':y,'left':x});
    
    });
    
    0 讨论(0)
  • 2021-01-06 06:38

    It should be pretty simple

    <div class="popup" > test</div>
    
    .popup
    {
     position:absolute;
     width:400px;
     height:400px;
    }
    

    jquery:

    get offset of the parent element , i mean click element

    $("#yourclickdiv").click(function (e) {
      var offset = $(this).offset();
      $(#popup).css('left',offset.left);    
      $(#popup).css('top',offset.top);
    });
    

    This should do it.

    0 讨论(0)
提交回复
热议问题