Getting wrong values from jquery .offset()

人走茶凉 提交于 2020-01-23 08:01:10

问题


I'm getting wrong values from jquery .offset(), I think it is because of CSS but cannot find the error.

This is the HTML element I'm trying to get the offset of:

<div id="sedes" class="seccion_in">
</div>

This is the CSS

.seccion_in { position:relative; overflow: hidden; width: 100%; height: auto; }

And this is the jquery:

console.log($("#sedes").offset());

And this is the working link: http://ficm.hacemoscodigo.com/asiste/

Thanks for your help.


回答1:


IDs MUST BE UNIQUE!

You have #sedes located in <div id="nav"> as well as a #sedes in <div id="content">. My guess is you are getting the .offset() of the #sedes in your nav. Remove/rename the ID that element and you should be good.

Make sure to use a classes instead.

Let me know if you have any questions!




回答2:


Review the jQuery API for offset.

It should be returning {top: 0, left: 0} because of the positioning of the parent.



来源:https://stackoverflow.com/questions/16092512/getting-wrong-values-from-jquery-offset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!