javascript - How to get xpos of a div's background image? -


i have simple script meant console.log xpos of div's background image console shows blank.

this have:

    var d = document.getelementbyid('div');     console.log(d.style.backgroundposition); //shows blank line     console.log(d.style.backgroundposition.xpos); //undefined (obviously) 

i'm curious why backgroundposition coming out blank though?

the div's css is:

 #div{     background-image: url('test.png');     background-repeat: no-repeat;     width:50px;     height:50px;     background-position: 0px 0px;  } 

the html requested:

<body>        <div id="div"></div> </body> 

any ideas ?

the format console.log(d.style.backgroundposition); works styles set inline. styles set via style sheet, use getcomputedstyle:

window.getcomputedstyle(d).getpropertyvalue("background-position"); // returns 0px 0px  

jsfiddle example

if want x or y position, use "background-position-x" or "background-position-y".


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -