我正在为一个网站的导航原型坐在窗口的底部。当我将鼠标悬停在链接上时,div会出现在带有子链接的导航上方,但在导航中,它并不位于悬停链接的中心位置。我试着用css来定位它- -50%向左,但效果很好,看起来它只向左移动了50%的父母大小,我该怎么用jQuery呢?
注意:我也在使用hoverIntent。
发布于 2010-12-22 00:36:18
定位(固定)一个1像素宽的容器div,然后将内部元素(您的实际项目)偏移其绝对宽度的一半。这个技巧对我很有效。
HTML:
<style type="text/css">
#testnav-container {
position:fixed;
bottom:0;
left:50%;
width:1px;
border:1px solid red;
text-align:center;
}
#testnav {
height:20px;
width:100px;
background:blue;
margin-left:-50px;
}
</style>
<div id="testnav-container">
<div id="testnav">test navigation bar</div>
</div>容器div上的1px边框只是为了告诉你它在哪里。试一试,告诉我它是怎么回事。
在控制台中运行的代码片段jQuery示例:
var foo = $('<div style="position:fixed;bottom:0;left:50%;width:1px;border:1px solid red;text-align:center;"><div style="height:20px;width:100px;background:blue;margin-left:-50px">test navigation bar</div></div>').appendTo('body');https://stackoverflow.com/questions/4501259
复制相似问题