-3

I'm having an issue with a jQuery selector in a variable. Oh and the HTML (background-image:url('SOME-IMAGE-URL')) has an inline style, not in a stylesheet.

I can't seem to make this JS/JQuery script. It completely breaks the script.

var contentHeight = $(this).css('background-image').height();

Full code:

var sliderWidth = 0;

$(".slider li").each(function() {
    var contentHeight = $(this).css('background-image').height();
    sliderWidth = sliderWidth + 100;
    $(".slider ul li").css("height",contentHeight);
});

var sliderContentWidth = sliderWidth/100;

$(".slider").css("width",sliderWidth + "%");
$(".slider li").css("width",100/sliderContentWidth + "%");

setInterval(function(){ 
    $(".slider ul").css("right",100/sliderContentWidth + "%");
}, 5000);

1 Answers1

3
$.css()

The above function (jQuery.css()) returns the CSS value for a given property; in your case a string. Strings don't have a .height() method though.

So on your line..

var contentHeight = $(this).css('background-image').height();

This is actually happens..

  1. $(this) is evaluated; and returns a jQuery instance.
  2. .css('background-image') is ran on the jQuery instance, and a string is returned (the image name)
  3. .height() is ran on the string... but it doesn't exist.. uh oh.

In your scenario though, the height of the element itself will probably suffice.

$(this).height() 

Especially considering that by default the background will just repeat if it's smaller than the element, and won't overflow if it's bigger.

Going beyond the question though, and to clarify after your comment below:

You want the size of the background image? This doesn't hold much use in reality. Take a look at the W3 documentation for background properties.

If the image is smaller than the element it's applied to, then the background-repeat property defaults to repeat. End result? The background takes on the whole element.

If the image is larger than the element it's applied to, then it's simply invisible outside the boundaries of the element. This is why sprites work. End result? The background takes on the whole element.

This behaviour can be changed using different properties, but in your scenario it doesn't appear to be. So essentially, the height of the element is the height of the background.

If you want to get the actual dimensions of the image then you're going to need to use a <img> element which, being a DOM element, DOES have height() and width() methods/properties. (Although this is going to require absolute positioning and possibly some z-indexing; and if you need to do this then it's probably best to re-think whatever you're doing)

Fergus In London
  • 1,203
  • 8
  • 19
  • I want to recieve the height of the css background-image – Jonas Rothmann Hjalager Jul 30 '13 at 21:18
  • I've explained this in my answer. You're setting the background image inline through CSS right? In which case the background-image size isn't relevant; as it will be repeated if it's too small - and will take up the whole element if it's too big. **By setting the background-image property of the element you are definitely going to have the whole element covered with a background; unless you use additional CSS properties such as ```background-size```; which you aren't.** – Fergus In London Jul 30 '13 at 21:59