2

I have a block of jquery / javascript code that sets the left margin of an image depending on the browser width.

I do some sums to calculate the required left margin and it goes in var $diff.

This code works fine:

$('#background-wrapper>img').attr('alt',$diff);

which demonstrates that the sums are all working fine as the image ends up with the correct value for $diff inserted in its alt attribute. This works on IE, FF, Chrome.

But if I change the code to:

    $('#background-wrapper>img').css('margin-left',$diff);

then firefox and chrome work fine, using $diff as a value for the images left-margin as I intended, but IE throws a run time error and stops running the script, citing an Invalid Argument in jquery file. I'm using jquery 1.3.2.min.

Any ideas?

Heres the code for the full function.

function imageResizeCenter() {
  var $windowh = jQuery(window).height();
  var $windoww = jQuery(window).width();

  var imagesrc = $('#background-wrapper2>img').attr('src');

  var myimage = new Image();

  myimage.src = imagesrc;
  var $width = myimage.width;
  var $height = myimage.height;

  var $ratio = parseInt($width,10)/parseInt($height,10);

  var $newwidth = parseInt($windowh,10)*$ratio;

  var $diff = (parseInt($windoww,10)-parseInt($newwidth,10))/2;
  if($diff<0) $diff=0;
  $('#background-wrapper2>img').attr('height',$windowh).css('margin-left',$diff);

}
2
  • Is there anything special about the image or the background-wrapper? I tried this exact script in IE6, IE7, IE8, and FF3, and I didn't get any errors. Commented Jan 26, 2010 at 3:33
  • i dont think theres anything odd... the background div is just a 100% width and height div, absolutely positioned to sit behind the rest of the site. The image is one of a number of images, all but one set to display:none. The end result is a slideshow of full screen (or large centered) images in the background of the site. I used images in a background div, rather than actual background-image css, so that I could make the images preload before transitioning to them with the slideshow. Commented Jan 27, 2010 at 11:06

2 Answers 2

1

Maybe

$('#background-wrapper>img').css('margin-left',$diff + 'px');
Sign up to request clarification or add additional context in comments.

5 Comments

+1. I would guess the lack of unit is an issue in some browsers.
Thanks for the response guys. I've just tried all of those suggestions, and chrome / ff both seem happy with all of those vaients, but IE is still giving the same error- even if I just change the property to marginLeft.
Can you post your javascript code and markup, or a snippet of it?
If you set the margin-left to a static value, does the error go away? Such as: $('#background-wrapper>img').css('margin-left','5px');
is it because ive used parseInt in the previous code but the .css function needs a string?
0

Using Javascript to set CSS properties has a little caveat: Instead of using dashes to separate multi-word properties, you use camelCasing. So margin-left should be marginLeft...

$('#background-wrapper > img').css('marginLeft', $diff);

1 Comment

Both dashed and camelCase version will work: "Also, jQuery can equally interpret the CSS and DOM formatting of multiple-word properties. For example, jQuery understands and returns the correct value for both .css('background-color') and .css('backgroundColor')." api.jquery.com/css

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.