简体   繁体   中英

position/offset of div returning as 0

Here is my css for my circle

#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:

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.

Why is this?

You need to set your position to "relative" or "absolute" before you can use top.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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