javascript - 一个链接中的 jQuery 定位错误

标签 javascript jquery html css

我一直在尝试使用 jQuery 创建一个菜单面板,可以通过单击顶部的预览 按钮在此处看到它:

$(function(){
  // hide all panels first
  $('div[id*="panel"]').hide();
  
  // make the panels absolute positioned
  $('div[id*="panel"]').css('position', 'absolute');
  
  // show the panel based on rel attribute of the current hovered link
  $('#menu a').hover(function(){
    var link_rel = $(this).attr('rel');

    //get the position of the hovered element
    var pos = $(this).offset();
    
    // set z-index of previous panels low
    $('div[id*="panel"]').css('z-index', '0');
    
    // get the panel near by hovered element now
    $('div#' + link_rel).css({
      'left': pos.left + 'px',
      'top': pos.top + 'px',
      'zIndex': '5000'
    });
    
    // finaly show the relevant hidden panel
    $('div#' + link_rel).fadeIn('slow');

    // hide it back when mouse arrow moves away
    $('div#' + link_rel).hover(function(){}, function(){
      $(this).fadeOut('slow');
    });

  }, function(){});
  
});

http://jsbin.com/amexi/edit

如果您将鼠标悬停在链接二链接三上,黑色面板会完美地替换相应的蓝色链接,但是如果您将鼠标悬停在链接一,黑色面板几乎不在该链接下方。这里出了什么问题?我该如何解决这个问题?

最佳答案

您需要考虑自动应用于 <ul> 的 margin 元素。

如果您使用 Firebug 查看您的页面,您会注意到 Firefox 应用了 16 像素的顶部和底部边距。

关于javascript - 一个链接中的 jQuery 定位错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2389104/

相关文章:

python - 是否有内置包将 html 解析为 dom?

javascript - setState 仅在使用对象作为状态时设置最后输入

javascript - ThreeJS 作为 AngularJS 范围对象 - requestAnimationFrame 不识别回调

javascript - 递延/ promise 相互依赖

jquery - 图表 JS : How to set units?

html - 联系人表单在 Firefox 中消失

javascript - 在条形图表中从底部向上提起条形图

javascript - 无法理解为什么使用展开运算符

javascript - 如何重置输入自动完成样式?

html - 无法使选择元素上的 rgba() 背景色在 Chrome 中工作