Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-20-2012, 08:25 PM   PM User | #1
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
using backgroundPosition-please help

Could someone please take a look at the script below. the code positions a background image at 0px -12px. But I need to replace these numbers with variables x and y which consequently, I will be changing dynamically.
I need to use x and y in the code, but I don't know how to do it properly.


Code:
<script type="text/javascript">
function ini() {
var list=document.getElementById("nav_list");
x=0;
y=-12;
list.style.backgroundPosition='0px -12px';
}

window.onload=ini;
</script>


for example

Code:
list.style.backgroundPosition='xpx ypx';  does not work
list.style.backgroundPosition=x+"px"  y+"px'; does not work either
thank you very much for help.

Last edited by chickentulip; 09-20-2012 at 08:28 PM..
chickentulip is offline   Reply With Quote
Old 09-20-2012, 08:40 PM   PM User | #2
Sammy12
Registered User

 
Join Date: Jun 2011
Posts: 1,063
Thanks: 12
Thanked 241 Times in 240 Posts
Sammy12 is on a distinguished road
Code:
list.style.backgroundPosition=x+"px"  y+"px'
Isn't that supposed to be "?

sec let me look at this
Sammy12 is offline   Reply With Quote
Old 09-20-2012, 08:44 PM   PM User | #3
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
Code:
list.style.backgroundPosition=x+"px"  y+"px';
no, this option does not work
chickentulip is offline   Reply With Quote
Old 09-20-2012, 08:47 PM   PM User | #4
Sammy12
Registered User

 
Join Date: Jun 2011
Posts: 1,063
Thanks: 12
Thanked 241 Times in 240 Posts
Sammy12 is on a distinguished road
Yeah, you needed " instead of ' and a space between the x and y values.

Code:
function ini() {
	x=0;
	y=-12;
	document.getElementById('nav_list').style.backgroundPosition = x + "px" + " " + y + "px";
}
window.onload=ini;
or with your original pursuit:

Code:
function ini() {
	var list=document.getElementById("nav_list");
	x=0;
	y=-12;
	list.style.backgroundPosition = x + "px" + " " + y + "px";
}
window.onload=ini;

Last edited by Sammy12; 09-20-2012 at 08:50 PM..
Sammy12 is offline   Reply With Quote
Users who have thanked Sammy12 for this post:
chickentulip (09-21-2012)
Old 09-21-2012, 02:38 PM   PM User | #5
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
it works. I would have never guessed that I have to make up for an extra space ....
thank you very much!
chickentulip is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:39 PM.


Advertisement
Log in to turn off these ads.