简体   繁体   English

将.position与CSS位置一起使用:相对不能按预期工作

[英]Using .position with css position: relative not working as expected

I have a menu that's not working right. 我的菜单无法正常工作。 have a look at this code: http://jsfiddle.net/PX9nU/1/ 看看这段代码: http : //jsfiddle.net/PX9nU/1/

On #menu li , if I leave position: relative in place, the console records a left of 0 across all menu items when you hover. #menu li ,如果我将position: relative保留在原位,则当您将鼠标悬停时,控制台会在所有菜单项上记录左0。 If I remove position: relative , it records the expected correct left value for each item. 如果我删除position: relative ,它将记录每个项目的预期正确左值。

I need the position: relative in place for the submenu to format correctly, but I need the left value to output correctly for some animation I'm trying to achieve. 我需要position: relative位置,以便子菜单正确设置格式,但是对于我要实现的某些动画,我需要左值正确输出。

-Why is the left not returning the expected value with position: relative in place? -为什么左派没有返回position: relative为期望值position: relative的?

-Is there another way to get the left value for each item as they are hovered over? -是否有另一种方法可以将鼠标悬停在每个项目上以获取其左值?

The problem is your jQuery selector. 问题是您的jQuery选择器。 You're selecting with $('#menu li .item') , which gives you back the link elements. 您正在使用$('#menu li .item') ,它可以使您返回链接元素。 Calling position() on those gives their position within their parent element, the enclosing LI, which is 0,0 in each case. 在这些元素上调用position()会给出其在其父元素(即封闭的LI)中的位置,在每种情况下均为0,0。 I think you want the position of the enclosing LI within its UL, which you can get this way: 我认为您希望封闭的LI在其UL中的位置,可以通过以下方式获得:

p = $(this).parent().position();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM