SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    background image width

    hi,
    im trying to get the size of a css defined background image. and set the tab01 width to the image width size. it works fine but is there a better way (i dont wanna do the .substr())

    thanks

    pete

    Code JavaScript:
    obj=document.getElementById('tab01').id;
     
    var backgroundImgSrc = getComputedStyle(obj,'').getPropertyValue('background-image');
     
    //i need to cut the url(), otherwise its not working with newImg.src
    var backgroundIm = backgroundImgSrc.substr(4,backgroundImgSrc.length-5);
     
    var newImg = new Image();
    newImg.src = backgroundIm;
    var width = newImg.width;
     
    document.getElementById('tab01').style.width = width + 'px';

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't think you have a choice - you're able to do it on one line anyway, I don't see what the concern is.

    BTW IE doesn't support getComputedStyle. You have to use obj.currentStyle.backgroundImage for it.


Bookmarks

Posting Permissions

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