【问题标题】:position/offset of div returning as 0返回为 0 的 div 的位置/偏移量
【发布时间】:2016-08-27 13:26:16
【问题描述】:

这是我圈子的css

#balloon-circle {
    width: 150px;
    height: 150px;
    background: rgb(255,100,100);
    -moz-border-radius: 150px;
    -webkit-border-radius: 150px;
    border-radius: 150px;
    top:5px;
}

这里是javascript:

var bc = document.getElementById("balloon-circle");
var bctop = bc.offsetTop;
console.log(bctop);

我也试过了:

var bc = $("#balloon-circle");
var position = bc.position();
console.log(position.top);

但是,它们都返回 0 作为顶部位置值。

这是为什么?

【问题讨论】:

    标签: javascript css position offset


    【解决方案1】:

    您需要将您的位置设置为“相对”或“绝对”,然后才能使用顶部。

    http://www.w3schools.com/css/css_positioning.asp

    #balloon-circle {
        width: 150px;
        height: 150px;
        background: rgb(255,100,100);
        -moz-border-radius: 150px;
        -webkit-border-radius: 150px;
        border-radius: 150px;
        position: relative;
        top:5px;
    }
    

    【讨论】:

    • fixed 也可以工作;)(只是任何非静态位置)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多