jquery $('selector').css('top') returns different values for IE, firefox and Chrome

后端 未结 2 685
南笙
南笙 2021-01-22 05:14

I am using the jQuery $(\'selector\').css(\'top\') to get the top value to help position dynamic elements and I find that I get different results based on the browser.

i

相关标签:
2条回答
  • 2021-01-22 05:30

    The thing is, none of your test elements actually have dynamic positioning. See this question: jQuery .css("left") returns "auto" instead of actual value in Chrome

    0 讨论(0)
  • 2021-01-22 05:31

    Use $('selector').offset().top to get the numeric value of the top position.
    .css() returns the CSS value of the top-position, which could be auto, 1234px, or something similar - not a reliable method to get the top position.

    See also:

    • http://api.jquery.com/offset/
    • http://api.jquery.com/position/
    0 讨论(0)
提交回复
热议问题