longtone Posted April 30, 2010 Share Posted April 30, 2010 I have a div #rotator, containing a number of images. I am using this jQuery code to get the dimensions of the div and the first image: //get size of div this.div_width = $("#rotator ").width(); this.div_height = $("#rotator ").height(); //get size of image this.img_width = $("#rotator img:eq(0)").width(); this.img_height = $("#rotator img:eq(0)").height(); sometimes it works, sometimes the image dimensions come back completely wrong eg: 14 instead of 200 Any ideas why, and how to fix it? Quote Link to comment https://forums.phpfreaks.com/topic/200276-jquery-width-attribute-error/ Share on other sites More sharing options...
yperevoznikov Posted May 3, 2010 Share Posted May 3, 2010 Hi Longtone, Probably images just don't load to moment you get its dimensions. Can you get these dimensions by .bind("load", method) for your image. I think you use OOP and you need bind method, so you can find how to bind method of class in my blog http://bit.ly/9nWaGV if you wish. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/200276-jquery-width-attribute-error/#findComment-1052613 Share on other sites More sharing options...
longtone Posted May 6, 2010 Author Share Posted May 6, 2010 Thanks, I'll try that I'm not sure that's the problem though, because I get the width error on the first image, which is loaded with the page, not the subsequent images loaded with JS And surely the first image should be fully loaded before $(document).ready calls any functions? I've got round it for now by making all the images the same size, but that does increase bandwidth and causes me unnecessary work... Quote Link to comment https://forums.phpfreaks.com/topic/200276-jquery-width-attribute-error/#findComment-1054023 Share on other sites More sharing options...
.josh Posted May 6, 2010 Share Posted May 6, 2010 I don't think $(document).ready waits for images to completely load... Quote Link to comment https://forums.phpfreaks.com/topic/200276-jquery-width-attribute-error/#findComment-1054073 Share on other sites More sharing options...
RichardRotterdam Posted May 6, 2010 Share Posted May 6, 2010 CV is right the ready function waits till the DOM is ready and not for the whole page to load $.ready() != window.onload Try something like the following : // wait for the while page to be loaded $(window).load(function(){ // your image script }); Quote Link to comment https://forums.phpfreaks.com/topic/200276-jquery-width-attribute-error/#findComment-1054089 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.