Aaron Prowse Aaron Prowse - 1 year ago 65
CSS Question

Jquery calculation with variables

Hi guys got a problem hopefully you can help,

var imageHeight = $("#slider > Img").height();
var imageFloat = (imageHeight + 31 / 2);



Can anyone see the issue here, the alert on
is bringing back a value of 15.5 which means the
is not being accounted for, The variable
is working and retrieving the correct number of the height image, also the margin is working and the
tag is being moved down by 15.5px. Dont know much about jQuery, I think this is just a syntax issue.

Answer Source

Most likely the img isn't fully loaded, so its height is 0.

To fix it, put an onload handler on the img. And also, check to see if it is cached using the .complete property, and of so, invoke the handler manually.

$("#slider > Img")
    .on("load", function() {
        var imageHeight = $(this).height();
        var imageFloat = (imageHeight + 31 / 2);


    .filter(function() { return this.complete; })

And keep in mind that / has higher precedence than +, so this:

var imageFloat = (imageHeight + 31 / 2);

is doing this:

var imageFloat = (imageHeight + (31 / 2));

instead of this:

var imageFloat = ((imageHeight + 31) / 2);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download