473,466 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I get the layers backgroundPosition in pixels?

When I added an image to a css layer, to center it horizontally, I
used 50%. Now, in my javascript, I need to be able to determine what
that x position is in pixels. How can I do this?

Jul 30 '07 #1
2 1631
Just to clarify, I added an image to a larger layer, and positioned
the image like this:
background-position: 50% 120px;

Now, in my javascript, I would like to find what the x coord is of
that background image.

Thanks.

Jul 30 '07 #2
OB*********@yahoo.com wrote:
The problem is, different images will be used for the background
image, and the width of the image will vary from width to width. So,
if there is a way to dynamically determine the current width of the
background image, I can use step 4 of your solution. [...]
There is a way, just not a completely reliable one. Quickhack:

/**
* @see http://pointedears.de/scripts/types.js
*/
function isMethodType(s)
{
return /\b(function|object)\b/i.test(s);
}

function _getComputedStyle(el, pseudoEl)
{
if (isMethodType(typeof getComputedStyle) && getComputedStyle)
{
return getComputedStyle(el, pseudoEl || null);
}
else if (typeof el.currentStyle != "undefined")
{
return el.currentStyle;
}
else
{
return {};
}
}

var compStyle = _getComputedStyle(targetElement);
if (compStyle && typeof compStyle.backgroundImage != "undefined"
&& isMethodType(typeof Image) && Image)
{
var img = new Image();
img.onload = function()
{
window.alert(img.width);
}

img.src =
compStyle.backgroundImage.replace(/(url\(\s*'?|'?\s*\))/gi, "");
}

(There is certainly a better, more standards compliant way for writing a
gCS() wrapper; I'm working on that.)

You can reduce the above to

if (isMethodType(typeof Image) && Image)
{
var img = new Image();
img.onload = function()
{
window.alert(img.width);
}

// feature test required, see dhtml.getStyleProperty()
img.src = targetElement.style.backgroundImage
.replace(/(url\(\s*'?|'?\s*\))/g, "")
}

if the target element's style attribute had been set with a
background-image declaration.
I've tried getting the x coordinate by using backgroundPositionX,
Never heard of that. Thanks.
but that seems to return "50%" (not a pixel value) in IE, and "undefined"
in Firefox.
Because it is MSHTML-only:

http://msdn2.microsoft.com/en-us/library/ms530719.aspx

However, parsing the backgroundPosition property for the first
white-space delimited part would show the same in Firefox.
PointedEars
Jul 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lizzy | last post by:
I have an internet application which has a progress bar show whenever the customer is requesting data from the server. This progress bar is coded to work in both Netscape 4.75 and above and IE 5.0...
6
by: David List | last post by:
I'm having a problem using different properties of the document object in the example javascripts in my textbook with browsers that identify themselves as using the Mozilla engine. One example of...
18
by: Timothy Casey | last post by:
Thanks in advance... =~= Timothy Casey South Australia worloq@iprimus.com.au Formerly: casey@smart.net.au
3
by: Crimefighter | last post by:
I'm not a javascript guy, but the use of this banner rotator script has given me fits. I know a few causes of the problem thus far, one being the layer tags aren't supported under Netscape 7.1...
4
by: ashkaan57 | last post by:
Hi, I am using the following code to show/hide part of an html page. It works in Netscape and Firefox but dies in IE: "Error: document.layers is null or not an object" <style> ..noshow {...
4
by: zz | last post by:
Sorry for posting this here but unfortunatly I couldn't find any appropriate newsgroup for this and since I've been active here with .net question I figured you guys might be able to help. ...
7
by: www.gerardvignes.com | last post by:
I was checking out a website offered as an example for another job, and I was struck by how clean and professional the site is. http://www.colorlines.com/ I was poking around, and I noticed...
5
by: Burt | last post by:
There's an architect at my 200 person company that advocates having many layers in all my C# apps. He wants web services, use case handlers, facade layers, data gateways, etc. When I ask why all...
1
by: Grimm | last post by:
I am developing an internalk inteface that integrates alot of seperate tools into one interface. The current version uses one Iframe inside a div layer for positioning. Currently the seperate web...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.