JavaScript/jQuery - offsetLeft and offsetTop

后端 未结 3 1966
余生分开走
余生分开走 2021-02-03 15:36

When hovering over a span I would like to get the offsetLeft and offsetTop values so I can make something hover near it. When I do this I get 0 for both values.

What is

相关标签:
3条回答
  • 2021-02-03 16:16

    $(this).offset().left and $(this).offset().top

    0 讨论(0)
  • 2021-02-03 16:17

    I think you should be able to do this:

    HTML

    <span class="get-close-to">some username</span><br />
    

    jQuery

    jQuery('.get-close-to').hover(function() {
        var offset = jQuery(this).css('offset');
        alert( 'Left: ' + offset.left + '\nTop: ' + offset.top );
    });
    
    0 讨论(0)
  • 2021-02-03 16:19

    Why do you need server-side scripting?

    try this:

        var positionImg = function(e) {
            $(this).offset();
        var zoomCntnrPos = $(this).offset.top;
          if (zoomCntnrPos >= maxBottomVPos)
          {
               tPosX = e.pageX;
               tPosY = e.pageY +20;
              }
          else if (zoomCntnrPos <= maxTopVPos){
               tPosX = e.pageX;
               tPosY = e.pageY +40;
              }
          else
              {
               tPosX = e.pageX;
               tPosY = e.pageY -100;  
              }
            $zoomContainer.css({top: tPosY, left: tPosX});
        };
    
    0 讨论(0)
提交回复
热议问题