Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2010
    Location
    Toronto
    Posts
    95
    Thanks
    52
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Registered User
    Join Date
    Jun 2011
    Posts
    1,063
    Thanks
    12
    Thanked 241 Times in 240 Posts
    Code:
    list.style.backgroundPosition=x+"px"  y+"px'
    Isn't that supposed to be "?

    sec let me look at this

  • #3
    New Coder
    Join Date
    Oct 2010
    Location
    Toronto
    Posts
    95
    Thanks
    52
    Thanked 0 Times in 0 Posts
    Code:
    list.style.backgroundPosition=x+"px"  y+"px';
    no, this option does not work

  • #4
    Registered User
    Join Date
    Jun 2011
    Posts
    1,063
    Thanks
    12
    Thanked 241 Times in 240 Posts
    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.

  • Users who have thanked Sammy12 for this post:

    chickentulip (09-21-2012)

  • #5
    New Coder
    Join Date
    Oct 2010
    Location
    Toronto
    Posts
    95
    Thanks
    52
    Thanked 0 Times in 0 Posts
    it works. I would have never guessed that I have to make up for an extra space ....
    thank you very much!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •