1

Hi guys got a problem hopefully you can help,

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

alert(imageFloat);

$("#slidernavright").css("marginTop",(imageFloat));

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

3
  • 1
    Have you tried with a lower case I ? $("#slider > img").height(); Commented Oct 24, 2013 at 21:45
  • So if you replaced alert(imageFloat); with alert(imageHeight);, it produces a number other than 0? Commented Oct 24, 2013 at 21:45
  • alert(imageHeight); produces the value of 241 Commented Oct 24, 2013 at 21:53

4 Answers 4

1

Are you computing this before or after window.onload? If the image hasn't completely loaded yet, jQuery can't retrieve the height.

window.onload = function() {
  var imageHeight = $("#slider > img").height();
  var imageFloat = (imageHeight + 31 / 2); // perhaps you meant (imageHeight+31)/2 ?
  $("#slidernavright").css("marginTop",imageFloat);
};
Sign up to request clarification or add additional context in comments.

2 Comments

erm not too sure but tried this didnt work I am loading the document like this ?$(document).ready(function(){ $("#slider > Img#1").fadeIn(500); startSlider(); sliderControl(); controlHeight(); });
The document is ready before the images are loaded - you have to wait for the window to load. You can also tie it to just the image load, but there are tricks to be careful of; window.onload is easier.
0

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);

        alert(imageFloat);

        $("#slidernavright").css("marginTop",(imageFloat));
    })
    .filter(function() { return this.complete; })
    .trigger("load");

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);

4 Comments

this is working as such but imageHeight is producing the value of 991 which is like the size of the whole document and not the size of the #slider > Img
@AaronProwse: Did you try my updated answer? I made it simpler. $(this).height() should give you the height of the image.
working thanks mate :) much appreciated! is there away of adapting this so that the imageFloat is dynamic and updated everytime the image size changes without refresh? because the slider image basically stretches to the screen so is that possible to have the margin top adjusting for browser size change difference without refreshing?
@AaronProwse: You mean like if the user adjusts the window size? If so, then put a "resize" handler on window that runs the same code. It will run as the user resizes the window.
0

Are you SURE imageHeight is being calculated properly? I just tested it with imageHeight = 5 and it worked fine for me, even without the order of operations stuff.

Comments

0

try using something like parseInt() -- var * = parseInt(imageHeight);

This will give the actual int value, usually when you get values like this .height() they return the string representation of the value and not the actual int value. I had an issue with this a few days ago.

Comments

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.