JQuery: keep sensitive div size for image container before fadeIn

I have a little jQuery that selects a random image from a folder and fades it inside a div on every page load. The problem is that the div menu just below the image goes into the empty space of the image before the image fades.

What is the best way to have an image div maintain height before the image fades out? CSS to "fill" a div before an image fades? Or upload a space .jpg before a random image disappears? (All random images have the same size 1000px width x 250px height).

I need to keep the design responsive, so the best solution is to preload an empty jpg and then fade out in a random image.

I did not include images and headers for simplicity, but this function displays an image with the .randomheader class:

JQuery

<script> $(document).ready(function(){ $('.randomheader').randomImage(); }); </script> <script> (function($){ $.randomImage = { defaults: { path: '/fullpathhere/headerimages/', myImages: ['image1.jpg' , 'image2.jpg' , 'image3.jpg'], myCaptions: ['Caption 1' , 'Caption 2' , 'Caption 3'] } } $.fn.extend({ randomImage:function(config) { var config = $.extend({}, $.randomImage.defaults, config); return this.each(function() { var imageNames = config.myImages; var imageCaptions = config.myCaptions; var imageNamesSize = imageNames.length; var randomNumber = Math.floor(Math.random()*imageNamesSize); var displayImage = imageNames[randomNumber]; var displayCaption = imageCaptions[randomNumber]; var fullPath = config.path + displayImage; // Load the random image $(this).attr( { src: fullPath, alt: displayImage }).fadeIn('slow'); // Load the caption $('#caption').html(displayCaption).fadeIn('slow'); }); } }); </script> 

HTML:

  <header id="masthead" class="site-header" role="banner"> <!-- random image loads in the div below --> <img src="" class="randomheader" width="1000" height="250" alt="header image"> <div id="caption"></div> <!-- The nav div below jumps up into the div above --> <nav id="site-navigation" class="main-navigation" role="navigation"> // nav here </nav> 

CSS

 .randomheader { margin-top: 24px; margin-top: 1.714285714rem; } img.randomheader { max-width: 100%; height: auto; display: none; } 
+6
source share
3 answers

@ apaul34208 pretty much hit the nail on my head ... I created an example for you on the same principle so that you can see how it works.

HTML:

 <header id="masthead" class="site-header" role="banner"> <div class="randomheader"> <div class="image"><img src="" /></div> <span class="caption"></span> </div> <nav id="site-navigation" class="main-navigation" role="navigation"> nav here </nav> </header> 

CSS

 .randomheader .image { position: relative; height: 0; /* This percentage needs to be calculated using the aspect ratio of your image. Type your image width and height in this tool to get the aspect ration of your image: http://andrew.hedges.name/experiments/aspect_ratio/ In this example the Google logo was 55:19 and to get the percentage you divide 19 by 55 = 0.3454 */ padding-bottom: 34.54%; } .randomheader img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .randomheader .caption { display: block; min-height: 1em; } .randomheader img, .randomheader .caption { opacity: 0; } 

JQuery

 (function($) { $.fn.extend({ randomImage: function(config) { var defaults = {}, config = $.extend({}, defaults, config); return this.each(function() { var randNum = Math.floor(Math.random()*config.myImages.length), image = config.myImages[randNum], caption = config.myCaptions[randNum], path = config.path + image, $container = $(this), $image = $container.find('img'), $caption = $container.find('.caption'); // preload image before adding // to DOM and fading it in $image.attr({ src: path, alt: image }).load(function() { $caption.html(caption); $image.add($caption).animate({ opacity: 1 }, 'slow'); }); }); } }); }(jQuery)); // on DOM ready, // start 'er up jQuery(function($) { $('.randomheader').randomImage({ myImages: ['logo4w.png'], myCaptions: ['Google logo'], path: 'https://www.google.co.uk/images/srpr/' }); }); 

JSFiddle Demo

+6
source

A jsfiddle I made shows how this can be done. The display:none style that you overlay on the image actually removes the element from the DOM, rather than just hiding it. Thus, the nav bar jumps up because there is really "nothing" before calling fadeIn .

You still have the image, but you need to either replace it with an empty element with the same height, or put it in a container, as in jsfiddle.

+3
source

I had a similar issue with my content jumping down while loading my slideshow.

Here is what I found - jsFiddle

HTML

 <div id="Container"> <div id="box"></div> <div id="element"> <!-- add content here --> </div> </div> 

CSS

 #Container { display: inline-block; position: relative; width: 100%; } #box { padding-top: 25%; /* adjust % for height */ } #element { position: absolute; top: 0; bottom: 0; left: 0; right: 0; background:red; /* added for visibility */ } 

It is great for storing liquids. Just play with percentages to get your desired size.

I first saw this method somewhere on SO, but I think it was an adaptation of this - http://ansciath.tumblr.com/post/7347495869/css-aspect-ratio

+3
source

All Articles