代码之家  ›  专栏  ›  技术社区  ›  user259752

获取位置+20px?

  •  0
  • user259752  · 技术社区  · 15 年前
        $(document).ready(function() { 
    
         //select all the a tag with name equal to modal
         $('a[name=popup]').click(function(e) {
          //Cancel the link behavior
          e.preventDefault();
    
          //Get the A tag
          var id = $(this).attr('href');
    
      //Set the popup window to center
      $(id).css('top',  winH/2-$(id).height()/2);
      $(id).css('left', winW/2-$(id).width()/2);
    

    我可以更改a href标记(top:dom)下面的弹出窗口吗?+20px)而不是中心??

    2 回复  |  直到 15 年前
        1
  •  0
  •   Nick Craver    15 年前

    你可以改变你的 top 对此:

    $(id).css('top', $(this).offset().top + 20);
    

    .offset() 返回单击链接的位置,您需要 顶部 值,这将返回单击链接的顶部,因此您可能还需要添加其高度,例如:

    $(id).css('top', $(this).height() + $(this).offset().top + 20);
    
        2
  •  0
  •   RobertPitt    15 年前
    $(document).ready(function() {  
    
        //select all the a tag with name equal to modal
        $('a[name=popup]').click(function(e) {
            //Cancel the link behavior
            //e.preventDefault(); //return false handles this
    
            //Get the A tag
            var id = $(this).attr('href');
            var position = $(this).position();
    
            //Set the top value to be 20px from the top
            $(id).css('top', (position.top + 20) + 'px');
    
            return false;
        }
    });
    

    http://api.jquery.com/position/