2010-08-25 121 views
0
$(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); 

可以我改变下面的一个href标记的弹出窗口(顶部:DOM + 20像素),而不是居中??获得位置+ 20像素?

回答

0

你可以改变你的top这样:

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

.offset()返回点击链接的位置,并且希望top值,这将返回被点击链接的顶部,因此您可能要添加它的高度为好,如:

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

我的意思是让一个href标记当前位置和+20 – user259752 2010-08-25 20:01:00

+0

@ user259752 - 啊,一会儿我会更新 – 2010-08-25 20:01:44

0
$(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/