简体   繁体   English

确定使用CSS移位的背景图像的左上角坐标

[英]Determining the top left coordinates of a background image that is shifted with CSS

I am trying to find the top and left coordinates of a background-image that by applying some CSS rules has been shifted off the viewport. 我试图通过应用一些CSS规则找到背景图像顶部和左侧坐标已从视口移开。 Difficult to explain in words, here is a visual example: 很难用文字解释,这里有一个视觉例子:

背景图像向左移动

Black box : Viewport 黑匣子 :视口
Red box : <div> with a background-image 红色框 :带background-image <div>
Blue box : <div> containing an <a> 蓝框<div>包含<a>

When I do getBoundingClientRect of the <div> with the background-image , I get 0px 0px . 当我使用background-image执行<div> getBoundingClientRect时,我得到0px 0px It makes sense, because the container is within the viewport, and it starts at the very top and left. 这是有道理的,因为容器在视口内,并且从最顶部和左侧开始。

However, the background-image of that <div> has been shifted to the left (and it could have been shifted to the top too), and therefore the coordinates should differ from the ones from the <div> . 但是, <div>的背景图像已经向左移动 (它也可能已经移到了顶部),因此坐标应该与<div>的坐标不同。 So my question is: 所以我的问题是:

How would I READ (I don't want to change) How can I find the coordinates of the green point in any page that is facing this situation? 我将如何阅读 (我不想更改)如何在任何面临这种情况的页面中找到绿点的坐标? I mean, the browser must have known how many pixels it needs to cut the background-image , right? 我的意思是,浏览器必须知道切割background-image需要多少像素,对吗?

I am currently using Javascript to access the Web/Dom API. 我目前正在使用Javascript访问Web / Dom API。 I am willing to use anything (undocumented maybe?) to achieve this. 我愿意使用任何东西(没有记录吗?)来实现这一目标。

Here is a solution to your problem that works on modern browsers. 这是适用于现代浏览器的问题的解决方案。

var testNode = document.getElementById('test');
var testBackgroundPosition = getComputedStyle(testNode,null).backgroundPosition.replace(/px/g,'').split(' ');

As you can see from the following page not all web browsers support this method. 从下一页可以看出,并非所有Web浏览器都支持此方法。

http://caniuse.com/getcomputedstyle http://caniuse.com/getcomputedstyle

There is no answer to the " Cross-browser (IE8-) getComputedStyle with Javascript? " question yet and I don't know another solution to this problem. 使用Javascript的跨浏览器(IE8-)getComputedStyle? ”问题尚未得到答案,我不知道这个问题的另一个解决方案。

Without getComputedStyle() there is no reasonable way of getting the current style settings for an element since that requires going through all of the included CSS. 没有getComputedStyle(),没有合理的方法来获取元素的当前样式设置,因为这需要遍历所有包含的CSS。 It is possible but involves CPU intensive code. 它是可能的,但涉及CPU密集型代码。 If you were to go that direction you will be able to create a temporary div inside the existing div with relative positioning, possibly setting top and left, or margins, to the values from the background position and then calculate where the div's clientTop and clientLeft ends up which may work in some cases. 如果你去那个方向,你将能够在现有div中创建一个临时div,它具有相对定位,可能设置top和left,或者从背景位置设置边距,然后计算div的clientTop和clientLeft结束的位置在某些情况下可能有效。

There is a css property for that: the background-position . 有一个css属性: 背景位置 Try the following code to retrieve the information asked for: 请尝试以下代码来检索要求的信息:

$('#divId').css('backgroundPosition');

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

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