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; </script>
HTML:
<header id="masthead" class="site-header" role="banner"> <img src="" class="randomheader" width="1000" height="250" alt="header image"> <div id="caption"></div> <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; }
source share