[英]How to get xpos of a div's background image?
我有一个简单的脚本,该脚本旨在console.log div的背景图像的xpos,但控制台显示空白。
这就是我所拥有的:
var d = document.getElementById('div');
console.log(d.style.backgroundPosition); //shows a blank line
console.log(d.style.backgroundPosition.xpos); //undefined (obviously)
我很好奇为什么backgroundPosition还是空白?
div的CSS是:
#div{
background-image: url('test.png');
background-repeat: no-repeat;
width:50px;
height:50px;
background-position: 0px 0px;
}
要求的html:
<body>
<div id="div"></div>
</body>
有任何想法吗 ?
格式console.log(d.style.backgroundPosition);
仅适用于内联设置的样式。 要通过样式表获取样式集,请使用getComputedStyle :
window.getComputedStyle(d).getPropertyValue("background-position"); // returns 0px 0px
如果只需要X或Y位置,请使用"background-position-x"
或"background-position-y"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.