Css Can';在Firefox和Safari中无法获得边距值

Css Can';在Firefox和Safari中无法获得边距值,css,firefox,safari,Css,Firefox,Safari,答案更新 从我的研究中得到自动在所有浏览器中的页边顶部我正在使用此代码 var bookContainer = $('#book-container'); bookContainer.offset().top 我不知道Firefox和Safari有什么问题 它警告0px不正确,但Chrome和IE可以执行正确的操作 使用相同的代码。我想问你知道发生了什么事吗 JS var bookContainer = $('#book-container'); alert(bookContai

答案更新

从我的研究中得到自动在所有浏览器中的页边顶部我正在使用此代码

var bookContainer   = $('#book-container');

bookContainer.offset().top

我不知道Firefox和Safari有什么问题

它警告0px不正确,但Chrome和IE可以执行正确的操作

使用相同的代码。我想问你知道发生了什么事吗

JS

var bookContainer   = $('#book-container');

alert(bookContainer.css('margin-top'))
#book-container{
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
    width: 80%;
    max-width: 830px;
    max-height: 520px;

    /*max-width: 748px;*/
    /*max-height: 469px;*/
}
CSS

var bookContainer   = $('#book-container');

alert(bookContainer.css('margin-top'))
#book-container{
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
    width: 80%;
    max-width: 830px;
    max-height: 520px;

    /*max-width: 748px;*/
    /*max-height: 469px;*/
}

试试这个它对我有用

var leftMarginValue= $('.container').offset().left;

因为您对div使用的是margin auto,所以元素的两侧都会有类似的margin。

试试这个,它对我很有用

var leftMarginValue= $('.container').offset().left;

因为您对div使用了margin auto。元素的两侧将得到类似的margin。

可能重复您将margin设置为auto,那么如何计算margin top。@gowtham我已经更新了我在问题中的答案,无论如何,谢谢你可能会重复你设置的自动边距,那么如何计算边距顶部。@gowtham我已经更新了我在问题中的答案,谢谢你