简体   繁体   English

div的位置/偏移返回为0

[英]position/offset of div returning as 0

Here is my css for my circle 这是我圈子的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;
}

And here is the javascript: 这是javascript:

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

I've also tried: 我也尝试过:

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

However, they both return 0 for the top position value. 但是,它们都返回0作为最高位置值。

Why is this? 为什么是这样?

You need to set your position to "relative" or "absolute" before you can use top. 您需要先将位置设置为“相对”或“绝对”,然后才能使用top。

http://www.w3schools.com/css/css_positioning.asp 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;
}

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

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